An Easy Task解题报告
2013-08-02 12:22
411 查看
题目摘要: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年一闰了,顿时有种世界观崩溃的感觉。忽然想起高中有个老师上课时说,到了大学你们会发现以前学的很多东西都是错的。哎,多么痛的领悟……
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年一闰了,顿时有种世界观崩溃的感觉。忽然想起高中有个老师上课时说,到了大学你们会发现以前学的很多东西都是错的。哎,多么痛的领悟……
相关文章推荐
- 解题报告 之 HDU5475 An easy problem
- POJ-1152 An Easy Problem! 解题报告(数论) 是不是N进制数
- 【解题报告】PKU 2826 An Easy Problem?!
- I - An Easy Problem!解题报告(来自网络)
- I - An Easy Problem!解题报告(张宇)
- HDOJ-1076 An Easy Task
- HDU 1076 An Easy Task
- hdoj 1076 An Easy Task
- leetcode解题报告(33): Find All Numbers Disappeared in an Array
- 【LeetCode】189.Rotate Array(Easy)解题报告
- LeetCode 384. Shuffle an Array 解题报告
- 杭电OJ(HDU)-ACMSteps-Chapter Two-《An Easy Task》《Buildings》《decimal system》《Vowel Counting》
- LeetCode解题报告 111. Minimum Depth of Binary Tree [easy]
- 【LeetCode】27.Remove Element(Easy)解题报告
- 【LeetCode】532. K-diff Pairs in an Array 解题报告
- HDU#1076:An Easy Task
- 【LeetCode】110. Balanced Binary Tree(Easy)解题报告
- LeetCode-Kth Largest Element in an Array-解题报告
- [leetcode] 421. Maximum XOR of Two Numbers in an Array 解题报告
- LeetCode解题报告 110. Balanced Binary Tree [easy]