请教杭电的1076 题,为什么代码这样写与样例输出的不对?谢谢

题目:
An Easy Task
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6477 Accepted Submission(s): 3896

Problem DescriptionIgnatius 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.

InputThe 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).

OutputFor 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 HintWe call year Y a leap year only if (Y%4==0 && Y0!=0) orY%400==0.

我的代码:
#include<stdio.h>
main()
{
int T;
scanf("%d",&T);
while(T--)
{
int y,n;
scanf("%d%d",&y,&n);
if((y%4==0&&y%100!=0)||y%400==0)
{
printf("%d\n",y+(n-1)*4);
}
else
{
while(1)
{
y++;
if((y%4==0&&y%100!=0)||y%400==0) break;
}
if(n==1) {printf("%d\n",y);}
else printf("%d\n",y+n*4);
}
}
}

总是闰年那个算不到,帮忙找找,谢谢

你误以为每4年一个闰年了。。。
比如说输入 2096这是一个闰年吧。但是2100年却不是一个闰年。
所以 y+(n-1)*4 这个公式完全就是错的,所以你得不出一个正确的答案
闰年:非世纪年,能被4整除的是闰年。在世纪年,能被400整除的才是闰年。
而你的公式。所有的世纪年都算成闰年了。。。
你可以用一个count计数,一年一年加,是闰年就count++,知道第n个闰年。然后输出count就行了
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网