HDOJ 1201 18岁生日

【思路】:无论如何都要经过18年,所以问题就是判断18年是否是闰年。注意:2月以后判断从下一年开始的18年,2月以前判断从今年开始的18年。另外还有一个2月29生日的特判。Ps.一年是365天,不是356天!

【AC代码】:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iomanip>
using namespace std;

int isLeapYear(int y)
{
    if ((y%4==0&&y%100!=0) || (y%400==0))
        return 1;
    return 0;
}

int main()
{
    int T = 0;
    cin >> T;
    while (T--)
    {
        int year = 0, month = 0, day = 0;
        int i = 0, days = 0;
        char c;
        cin >> year >> c >> month >> c >> day;
        if (2 == month && 29 == day && 0 == isLeapYear(year+18))
        {
            cout << -1 << endl;
            continue;
        }
        if (month > 2)
            year += 1;
        for (i = 0; i < 18; i++)
        {
            if (isLeapYear(year+i))
                days += 366;
            else
                days += 365;
        }
        cout << days << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值