您的位置:首页 > 其它

An Easy Task解题报告

2013-08-02 12:22 363 查看
题目摘要: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年一闰了,顿时有种世界观崩溃的感觉。忽然想起高中有个老师上课时说,到了大学你们会发现以前学的很多东西都是错的。哎,多么痛的领悟……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  解题报告