18岁生日
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 36267 Accepted Submission(s): 11755
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年中全为平年,计算其中的闰年数,在看是第一年经过2月29日还是最后一年经过2月29日,分情况计算。注意-1的情况就好。
#include <stdio.h>
int f(int x)
{
if((x % 400 == 0) || (x % 100 != 0 && x % 4 == 0))
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int yy,mm,dd;
int fy,ly;
int cont;
int ans;
scanf("%d-%d-%d",&yy,&mm,&dd);
fy = f(yy),ly = f(yy + 18);
cont = ans = 0;
for(int i = 1;i <= 17;i++)
{
if(f(yy + i))
cont++;
}
if(mm == 2 && dd == 29 && !f(yy + 18))
{
printf("-1\n");
continue;
}
if(mm >= 3)
{
ans = 18 * 365 + cont + ly;
}
else
{
ans = 18 * 365 + cont + fy;
}
printf("%d\n",ans);
}
return 0;
}