【水】HDU 2005 第几天

Description
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input

1985/1/20
2006/3/12

Sample Output

20
71

Hint
JGShining
Source
  C语言程序设计练习(一)  
Related problem
2010 2000 2012 2003 2004

这道题有个问题是关于闰年的计算,末尾为00的年份能被400整除的、末尾非00能被4整除的即为闰年。如2000,2004等,而2100不是闰年,这里可以给出一个算法:

year%400==0 || year%4==0 && year%100 != 0;

当该条件为真时,year即为闰年。

而闰年仅影响2月的天数,所以只需在累计2月天数时来个分支,加28天,或是加29天。

并且题目直接提示所输入的数据皆为合法的,基本没什么后顾之忧了。

代码如下:

#include <iostream>
using namespace std;

int main ()
{
int y,m,d;
char a,b;    //输入格式中有‘/’
while (cin >> y >> a >> m >> b >> d )
{
int sd=0;
switch (m)
{
    case 12:sd+=30;
    case 11:sd+=31;
    case 10:sd+=30;
    case 9:sd+=31;
    case 8:sd+=31;
    case 7:sd+=30;
    case 6:sd+=31;
    case 5:sd+=30;
    case 4:sd+=31;
    case 3:if(y%400==0||y%4==0&&y%100!=0)
                    sd+=29;
            else
                sd+=28;
    case 2:sd+=31;
    case 1:sd+=d;
}
    cout << sd <<endl;
}
}

猜你喜欢

转载自blog.csdn.net/qq_41627235/article/details/82780776