算法練習題1:第N個閏年是?

題目

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;
            }
        }

    }
}

測試用例

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章