先对程序做一下简单的分析:以10月21日为例,要先把前9个月的天数加起来,然后再加上21天,以为到这就完了吗?那如果是闰年呢?不要急,只要在判断一下就好了(还有小伙伴不会判断吗,下面有些写哦),如果是,那就再加上一天(大于2月的才能加),加油加油
#include<stdio.h>
int main(void)
{
int year, month, day, sum,flag ;
printf("\nplease input year,month,day\n");
scanf("%d,%d,%d", &year, &month, &day);
switch (month)
{
case 1: sum = 0; // 注意case与1之间有一个空格哦
break;
case 2:
sum = 31;
break;
case 3:
sum = 59;
break;
case 4:
sum = 90;
break;
case 5:
sum = 120;
break;
case 6:
sum = 151;
break;
case 7:
sum = 181;
break;
case 8:
sum = 212;
break;
case 9:
sum = 243;
break;
case 10:
sum = 273;
break;
case 11:
sum = 304;
break;
case 12:
sum = 334;
break;
default:
printf("data error");
break;
}
sum = sum + day;//不要忘了加天数
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))//判断闰年:能被400整除或能被4整除但不能被100整除。
flag = 1;//flag为标志变量
else
flag = 0;
if (flag == 1 && month > 2)
sum++;
printf("It is the %dth day.", sum);
return 0;
}