2012/03/04

[zerojudge] d124 3的倍數

題目:https://zerojudge.tw/ShowProblem?problemid=d124
說明:Java 中將 char 轉為 int 後再來判斷,若各數字的總和為 3 的倍數,則該數亦為 3 的倍數 ,Python 可以用 mod 3 來運算。

Java 版
import java.util.Scanner;

public class D124 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            char[] cs = in.nextLine().toCharArray();
            int sum = 0;
            for (char c : cs) {
                sum += (c - 48);
            }
            if(sum % 3 == 0)
                System.out.println("yes");
            else
                System.out.println("no");
        }
    }
}
Python 版 (2022.07)
while True:
    try:
        a = int(input())
        if a % 3 == 0:
            print('yes')
        else:
            print('no')
    except EOFError:
        break

沒有留言:

張貼留言