An Easy Task解题报告

题目摘要:Ignatius wasborn in a leap year, so he want to know when he could hold his birthday party.Can you tell him?

Given a positive integers Y which indicate the start year, and a positiveinteger N, your task is to tell the Nth leap year from year Y.
Note: if year Y is a leap year, then the 1st leap year is year Y.

 

题目大意:给出一个年份Y,一个N,求从Y年开始第N个闰年。

输入输出要求

Input

The input contains several test cases. Thefirst line of the input is a single integer T which is the number of testcases. T test cases follow.
Each test case contains two positive integers Y and N(1<=N<=10000).

 

Output

For each test case, you should output theNth leap year from year Y.

 

输入输出样例

Sample Input

3

2005 25

1855 12

2004 10000

 

Sample Output

2108

1904

43236

 

解题思路:定义一个计数器num。先对Y判断,如果Y是闰年,num赋值为1,即Y是第一个闰年。然后对Y循环加一,每有一个闰年,num加一直到num和N相等,此时Y即为所求。

代码

#include<iostream>

using namespace std;

int Judge(int y)

{

    if((y%4==0&&y%100!=0)||(y%400==0))

        return 1;

    return 0;

}

int main()

{

int T;

    cin>>T;

    while(T--)

    {

        int Y,N;

        int num=0;

        cin>>Y>>N;

        if(Judge(Y))

        {

            num=1;

        }

        while(num!=N)

        {

            Y++;

            if(Judge(Y))

                num++;

        }

        cout<<Y<<endl;

     }

    return 0;

}

解题感想:一直以为四年一闰,最后一个样例想了一晚上也没想明白为什么以2004年为第一个闰年,第10000个闰年是43236年,总觉得应该是42000年。后来发现,2096是闰年,2104是闰年,但2100不是闰年(2100不符合判断公式)。这就8年一闰了,顿时有种世界观崩溃的感觉。忽然想起高中有个老师上课时说,到了大学你们会发现以前学的很多东西都是错的。哎,多么痛的领悟……

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值