闰年:HDOJ 1076 An Easy Task

Problem Description

Ignatius was born 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 positive integer 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.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. 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 the Nth leap year from year Y.

Sample Input

3 
2005 25 
1855 12 
2004 10000

Sample Output

2108 
1904 
43236*Hint*
We call year Y a leap year only if (Y%4==0 && Y % 100!=0) or Y % 400==0.

这道题中闰年不考虑大数情况,即3200年其实不是闰年,但在题目中看做闰年。
然后本想着400年一个大循环,里面有24*4+1=97个闰年,但是写的代码老是运行错误,就写了个依次判断闰年的循环,比较慢,但是通过了。即下面C代码中的前部分:

#include "stdio.h"
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        int a,b,b1;
        int ans;
        scanf("%d %d",&a,&b);
        b1=b;
        ans=a;
        while(b > 0)
        {
            if ((ans % 4==0 && ans % 100=0) ||ans % 400==0)
                b--;
            ans++;
        }
        printf("%d\n",ans-1);
        b=b1;
        ans=a+(b/97)*400;
        b=b % 97;
        if(b==0)
            printf("%d\n",ans-4);
        else
        {
            while(b > 0)
            {
                if ((ans%4==0 && ans % 100=0) ||ans % 400==0)
                    b--;
                ans++;
            }
            printf("%d\n",ans-1);
        }
    }
}

你可能感兴趣的:(闰年:HDOJ 1076 An Easy Task)