给出一个日期 计算当天是该年的第几天
看了参考答案之后 至于闰年怎么定义 就不讨论了
我的心情是这样的:笨笨笨笨笨
1 #include <stdio.h> 2 int leap(); 3 int main() 4 { 5 int year,month,day,sum; 6 int leapYear[12] = {31,29,31,30,31,30,31,31,30,31,30,31}; 7 int NleapYear[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; 8 while(scanf("%d/%d/%d",&year,&month,&day)) 9 { 10 sum = 0; 11 switch (leap(year)) { 12 case 0: 13 while(month > 1) 14 { 15 month --; 16 sum += NleapYear[month - 1]; 17 } 18 sum += day; 19 break; 20 default: 21 while(month > 1) 22 { 23 month --; 24 sum += leapYear[month - 1]; 25 } 26 sum += day; 27 break; 28 } 29 printf("%d ",sum); 30 } 31 32 return 0; 33 } 34 35 int leap(int a) 36 { 37 if(a /400 == 0 || (a /4 == 0 && a /400 != 0)) 38 return 1; 39 else 40 return 0; 41 }
参考c++
1 #include <math.h> 2 #include <stdio.h> 3 4 #define lev(n) (n % 4 == 0 && (n % 100 != 0 || n % 400 == 0)) 5 6 int main(void) 7 { 8 int y, m, d, i, s; 9 int month[2][13] = { 10 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 11 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} 12 }; 13 14 while (scanf("%d/%d/%d", &y, &m, &d) != EOF) 15 { 16 for (s = 0, i = 1 ; i < m ; i++) 17 s += month[lev(y)][i]; 18 s += d; 19 printf("%d ", s); 20 } 21 22 return 0; 23 }