题目
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.
问题描述
Ignatius出生在闰年,所以他想知道什么时候可以举办他的生日派对。你能告诉他吗?
给定表示起始年份的正整数Y和正整数N,您的任务是告诉从Y年开始的第N个闰年。
注意:如果Y年是闰年,那么第一个闰年是Y年。
输入
输入包含几个测试用例。输入的第一行是单个整数T,它是测试用例的数量。 T测试案例如下。
每个测试用例包含两个正整数Y和N(1 <= N <= 10000)。
产量
对于每个测试用例,您应该从Y年输出第N个闰年。
样本输入
3
2005年25
1855年12月
2004年10000
样本输出
2108
1904
43236
暗示
只有当(Y%4 == 0 && Y%100!= 0)或Y%400 == 0时,我们才将Y年称为闰年。
作者
Ignatius.L
答案
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static boolean isLeapYear (int year) {
if ((year % 4 == 0 && year % 100 != 0 )|| year % 400 == 0)
return true;
return false;
}
public static int closeLeapYear (int year) {
if (isLeapYear(year)) return year;
int resultYear = year;
for (int i = 0; i < 8; ++i) {
resultYear = year + i;
if (isLeapYear(resultYear)) {
break;
}
}
return resultYear;
}
public static int resultYear(int year,int n) {
int resultYear = closeLeapYear(year);
for (int i = 0 ; i != n; ) {
boolean isLeap = isLeapYear(resultYear + 4 );
if (isLeap) {
i++;
}
resultYear = resultYear + 4;
}
return resultYear - 4;
}
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int times = 0;
int[] results = new int[0];
int index = 0;
int total = 0;
while(in.hasNext()) {
String a = in.nextLine();
if (!a.contains(" ")) {
total = Integer.valueOf(a);
times = total;
if (times < 1) {
break;
} else {
continue;
}
}
if (results.length < 1) {
results = new int[total];
}
if (times > 0) {
String[] result = a.split(" ");
String year = result[0];
int yearInt = Integer.valueOf(year);
String n = result[1];
int nInt = Integer.valueOf(n);
int resultYear = resultYear(yearInt,nInt);
results[index++] = resultYear;
times--;
}
if (times == 0){
for (int i = 0; i < results.length; i++) {
System.out.println(results[i]);
}
break;
}
}
}
}
网友评论