18岁生日
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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岁那年是闰年的时候,TA的生日是在2月29之前还是之后。
#include<stdio.h>
int Judge(int year)
{
if((year%4==0&&year%100!=0) || year%400==0)
return 1;
else return -1;
}
int main()
{
int i,Y,year,month,day,T;
int total=0;
scanf("%d",&T);
while(T--){
scanf("%d-%d-%d",&year,&month,&day);
if(Judge(year)&&month==2&&day==29&&Judge(year+18)==-1){
printf("-1\n");
continue;
}
else{
Y=year;
for(;year<=Y+17;year++){
if((month<2)||(month==2&&day<=28))
{
if(Judge(year)==1) total+=366;
else total+=365;
}
else{
if(Judge(year+1)==1) total+=366;
else total+=365;
}
}
printf("%d\n",total);
total = 0;
}
}
return 0;
}