2012/02/28

[zerojudge] d071 格瑞哥里的煩惱 (EOF 版)

題目:https://zerojudge.tw/ShowProblem?problemid=d071
說明:以 Java 及 Python 來說,與 d070 一模一樣程式碼即可解,也可以在 except 部分作 EOF 條件判斷。

Java 版
import java.util.Scanner;
 
public class D071 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int a = Integer.parseInt(in.nextLine());
            if(a == 0)
               break;
            else if(((a % 4) == 0 && (a % 100) != 0) || (a % 400) == 0)
                System.out.println("a leap year");
            else
                System.out.println("a normal year");
        }
    }
}
Python 版 (2022.07)
while True:
    try:
        a = int(input())
        if ((a % 4 == 0) and (a % 100 != 0)) or (a % 400 == 0):
            print('a leap year')
        else:
            print('a normal year')
    except EOFError:
        break

沒有留言:

張貼留言