HDU 1201 18岁生日

查看原题

题意

给出一个日期,算到十八岁时经过几天

思路

四年一闰,百年不闰,四百年再闰。

本年闰年:
* 在2/29日之前生日:到明年经过366天
* 在2/29日之后生日:到明年经过365天
本年平年:
* 来年闰年:
* * 在2/29日之前生日:到明年经过365天
* * 在2/29日之后生日:到明年经过366天
* 来年平年:到明年经过365天
2/29出生:18年后如果不是闰年,输出-1

代码

#include <iostream>
using namespace std;
int isrun(int year){
    if((year%4==0&&year%100!=0)||(year%400==0)){
        return 1;
    }
    else{
        return 0;
    }
}
int days(int year,int month,int day){
    int s=0;
    if(isrun(year)==1){
        if((month==1)||(month==2&&day<29)){
            s+=366;
        }
        else s+=365;
    }
    else{
        if(isrun(year+1)==1){
            if((month==1)||(month==2&&day<29)){
                s+=365;
            }else s+=366;
        }else s+=365;
    }
    return s;
}
int main(int argc, char *argv[])
{
    int n;cin>>n;
    while(n--){
        int year,month,day;
        int i;
        int sum=0;
        scanf("%d-%d-%d",&year,&month,&day);
        if((isrun(year+18)!=1)&&month==2&&day==29){
            cout<<-1<<endl;
        }else{
            for(i=0;i<18;i++){
                sum+=days(year,month,day);
                year++;
            }
            cout<<sum<<endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值