004 输入某年某月某日,判断这一天是这一年的第几天?
题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
程序源代码:
1 #include<stdio.h>
2 int main()
3 {
4 int year, month, day;
5 int flag;
6 int base, sum;
7 printf("请输入年月日:\n");
8 scanf("%d,%d,%d",&year,&month,&day);
9 //先判断是不是闰年
10 if(year%400==0||(year%4==0&&year%100!=0))
11 flag = 1;
12 switch(month)
13 {
14 case 1: base = 0; break;
15 case 2: base = 31; break;
16 case 3: base = 31+28; break;
17 case 4: base = 31*2+28; break;
18 case 5: base = 31*2+28+30; break;
19 case 6: base = 31*3+28+30; break;
20 case 7: base = 31*3+28+30*2; break;
21 case 8: base = 31*4+28+30*2; break;
22 case 9: base = 31*5+28+30*2; break;
23 case 10: base = 31*5+28+30*3; break;
24 case 11: base = 31*6+28+30*3; break;
25 case 12: base = 31*6+28+30*4; break;
26 }
27 if(flag == 1 && month > 2)
28 sum = base + day + 1;
29 else
30 sum = base + day;
31 printf("%d的第%d天\n",year,sum);
32
33 return 0;
34 }
运行结果: