题面
/*
编写一程序P713.C实现以下功能
用scanf输入某年某月某日,判断这一天是这一年的第几天?以3月5日为例,应该先把前两个月的加起来,然后
再加上5天即本年的第几天,特殊情况,闰年且输入月份≥3时需考虑多加一天。
注:判断年份是否为闰年的方法——为400的倍数为闰年,如2000年;若非100的倍数,而是4的倍数,为闰年,如1996年。
编程可用素材:
printf("Please input year-month-day: ");
printf("\nIt is the ...th day.\n");
程序的运行效果应类似地如图1所示,图中的红色部分是从键盘输入的内容。
Please input year-month-day: 2000-3-1
It is the 61th day.
*/
解答
#include<stdio.h>
int main(void)
{
int year,month,day;
int total=0;
printf("Please input year-month-day: ");
scanf("%d-%d-%d",&year,&month,&day);
switch(month)
{
case 12:
total +=30;
case 11:
total +=31;
case 10:
total +=30;
case 9:
total +=31;
case 8:
total +=31;
case 7:
total +=30;
case 6:
total +=31;
case 5:
total +=30;
case 4:
total +=31;
case 3:
total +=28;
case 2:
total += 31;
case 1:
break;
default:
break;
}
total += day;
if(year%400==0||(year%4==0&&year%100!=0))
{
if(month>=3)
{
total++;
}
}
printf("\nIt is the %dth day.\n",total);
return 0;
}
知识点
- 闰年的判断,多重逻辑组合条件的写法
- switch语句的最常见的用法,要思考,这里为何没有使用break语句
- 对于天数的累加,要特别注意 闰年的2月,是不是已经纳入到计算中