7-19 计算天数 (15分)
本题要求编写程序计算某年某月某日是该年中的第几天。
输入格式:
输入在一行中按照格式“yyyy/mm/dd”(即“年/月/日”)给出日期。注意:闰年的判别条件是该年年份能被4整除但不能被100整除、或者能被400整除。闰年的2月有29天。
输出格式:
在一行输出日期是该年中的第几天。
输入样例1:
2009/03/02
输出样例1:
61
输入样例2:
2000/03/02
输出样例2:
62
#include<stdio.h>
int main()
{
int yy,mm,dd;
int flag=0;
int t=0;
scanf("%d/%d/%d",&yy,&mm,&dd);
if(yy%4==0&&yy%100!=0||yy%400==0)
flag=1;
switch(mm-1)
{
case 11:t+=30;
case 10:t+=31;
case 9:t+=30;
case 8: t+=31;
case 7: t+=31;
case 6:t+=30;
case 5:t+=31;
case 4:t+=30;
case 3: t+=31;
case 2: t+=28+flag;
case 1: t+=31;
case 0: ;
}
t+=dd;
printf("%d\n",t);
return 0;
}
这是一道简单利用switch的特性来求的一道题,别忘记闰年的作用判断