题目及翻译
题面
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.
翻译
Ignatius 出生在闰年,所以他想知道他什么时候可以办生日派对,你能告诉他吗?
给出一个整数Y作为出生年,一个整数N,你的任务是说出从Y年开始的第N个闰年
注意:如果Y年本身是闰年,那么它本身作为N个闰年中的第一个
输入
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).
翻译
在输入的第一行有一个整数T代表输入样例的个数,每个样例有Y和N两个整数。
(1<=N<=10000)
输出
For each test case, you should output the Nth leap year from year Y.
翻译
对于每组样例,输出从Y年开始的第N个闰年
输入样例
3
2005 25
1855 12
2004 10000
输出样例
2108
1904
43236
闰年判定方法:能被400整除。或者能被4整除但不能被100整除。
题目思路
判断闰年+递增直到到达所需年份即可。
注意事项
如果Y年本身是闰年,那么它本身作为N个闰年中的第一个,这里要特判一下。
AC代码
C/C++(几乎没有代码变更)
用时0MS 内存1208K 长度621B
#include<stdio.h>
bool isLeapYear(int Year) {//判断闰年
if(Year % 4 == 0 && Year % 100 != 0)return true;//能被4整除但不能被100整除
if(Year % 400 == 0)return true;//能被400整除
return false;
}
int main() {
static int T,Now,Next;
scanf("%d",&T);
while(T--) {
scanf("%d %d",&Now,&Next);
while(!isLeapYear(Now))++Now;//如果不是闰年,先到达最近的一个闰年
--Next;//由于目前Now是闰年,作为第一个闰年,Next减少一次
while(Next > 0) {//循环知道Next到达0,就得到了所需的年份
Now += 4;//每次递增4年
if(isLeapYear(Now))--Next;//只有闰年才计数
}
printf("%d\n",Now);
}
return 0;
}
Java
网吧没有环境不好写呢⊙﹏⊙
本文作者 CSDN@扶她小藜
个人主页链接 https://blog.csdn.net/weixin_44579869