hdu1201——18岁生日

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

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
bool check(int year){
    if(year%4==0 && year%100!=0 || year%400==0){
        return true;
    }
    else{
        return false;
    }
}
int main(void){
    int n;
    int year,month,day;
    scanf("%d",&n);
    while(n--){
        scanf("%d-%d-%d",&year,&month,&day);
        int ans=0;
        if(month==2 && day==29){
            printf("-1\n");
            continue;
        }
        if(month<3){
            for(int i=0;i<18;i++){
                if(check(year+i)){
                    ans+=366;
                }
                else{
                    ans+=365;
                }
            }
        }
        else{
            for(int i=1;i<=18;i++){
                if(check(year+i)){
                    ans+=366;
                }
                else{
                    ans+=365;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/westbrook1998/article/details/81274295