18岁生日
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21799 Accepted Submission(s): 6904
Problem Description
Gardon的18岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达18岁生日时所经过的天数都是一样的呢?似乎并不全都是这样,所以他想请你帮忙计算一下他和他的几个朋友从出生到达18岁生日所经过的总天数,让他好来比较一下。
Input
一个数T,后面T行每行有一个日期,格式是YYYY-MM-DD。如我的生日是1988-03-07。
Output
T行,每行一个数,表示此人从出生到18岁生日所经过的天数。如果这个人没有18岁生日,就输出-1。
Sample Input
1
1988-03-07
Sample Output
6574
//
该题实际上就是求18年内闰年个数以及第一年与最后一年分别为闰年时日期是在02-29(闰年)之前还是之后。另外,隐含条件为第一年与第十八年不同时为闰年
//
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int month,year,date,days,i;
int T;
char Null;
scanf("%d",&T);
while(T--)
{
days = 0;
cin >> year >> Null >> month >> Null >> date;
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
if( month ==2 && date == 29) {cout << "-1" << endl; continue;}
else if(month <= 2) days++;
}
else
if((year + 18) % 400 == 0 || (year + 18) % 4 == 0 && (year + 18) % 100 != 0)
{
if(month > 2) days++;
}
i = year + 1;
while(i < year + 18)
{
if(i % 400 == 0 || (i % 4 == 0 && i % 100 != 0)) days++;
i++;
}
days += 18*365;
cout << days << endl;
}
}