求算法解释
#include <stdio.h>int main()
{
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i, year;
printf("请输入一个年份:");
scanf("%d", &year);
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) //这一句的算法完全懵逼,拆开看得懂合在一起完全不懂尤其是year % 100 != 0
{
days = 29;
}
for (i = 0; i < sizeof(days) / sizeof(days); i++) //取days长度 ÷ days?? 这是什么操作?
{
printf("%2d月份:%d天\n", i+1, days);
}
return 0;
} if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) //算闰年的规则
{
days = 29;
}
for (i = 0; i < sizeof(days) / sizeof(days); i++) //算数组里的元素个数(数组总大小/一个元素大小),其实不用这么麻烦,直接给12就行(i<12)
{
printf("%2d月份:%d天\n", i+1, days);
}
页:
[1]