题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2005
问题描述:
第几天?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 146927 Accepted Submission(s): 52672
Problem Description
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20 2006/3/12
Sample Output
20 71
Author
lcy
题目分析:输入年月日,输出这个日期是该年的第多少天。
解题思路:用switch,case实现,循环月数-1次。
代码实现:
#include<stdio.h>
int main (void)
{
int year,month,day,n;
while(scanf("%d/%d/%d",&year,&month,&day)!=EOF)
{
n=0;
switch(month-1)
{
case 11:n+=30;
case 10:n+=31;
case 9:n+=30;
case 8:n+=31;
case 7:n+=31;
case 6:n+=30;
case 5:n+=31;
case 4:n+=30;
case 3:n+=31;
case 2:if(year%4==0 && year%100!=0 || year%400==0) //判断闰年
n+=29;
else
n+=28;
case 1:n+=31;
}
printf("%d\n",n+day);
}
return 0;
}