Problem Description
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20 2006/3/12
Sample Output
20 71
水题,不过有几点需要注意:
1.需要判断是不是闰年。
2.这里需要一个小技巧来“吃掉”输入中的/,定义一个字符型变量存储即可。
3.注意逻辑结构,我第一次写的时候逻辑错了,错误代码给出,代码中注释的部分。
4.我还有一点错误的地方:||的错误用法
month==1||3||5||7||8||10||12
代码如下:
#include <iostream>
using namespace std;
int isLeapYear(int year)
{
if((year%4==0&&year%100!=0)||(year%400==0))
return 1;
else
return 0;
}
int month_days(int year,int month)
{
int monthdays;
/*if(month==1||3||5||7||8||10||12)
monthdays=31;
else if(month!=2) 逻辑错了,!=2是包含第一个if里的情况的。
monthdays=30;
else if(month==2)
{
if(isLeapYear(year))
monthdays=29;
else
monthdays=28;
}
return monthdays;*/
if(month!=2)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
monthdays=31;
else if(month==4||month==6||month==9||month==11)
monthdays=30;
}
else
{
if(isLeapYear(year))
monthdays=29;
else
monthdays=28;
}
return monthdays;
}
int main()
{
int year,month,day,dayth=0;
char eat;
while(cin>>year>>eat>>month>>eat>>day)
{
for(int i=1;i<month;i++)
{
dayth+=month_days(year,i);
}
dayth+=day;
cout<<dayth<<endl;
dayth=0;
}
}