2012/02/28

[zerojudge] d073 分組報告

題目:https://zerojudge.tw/ShowProblem?problemid=d073
說明:使用除法及取餘數來計算,Python 中第一次使用到一行版的 if/else,居然是將 expression 寫在 if 前面,好特別。

Java 版
import java.util.Scanner;

public class D073 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int a = in.nextInt();
            int r = (a / 3) + (a % 3 == 0 ? 0 : 1);
            System.out.println(r);
        }
    }
}
Python 版 (2022.07)
a = int(input())
r = a // 3 + (0 if a % 3 == 0 else 1)
print(r)

沒有留言:

張貼留言