2012/02/13

[zerojudge] a020 身分證檢驗

題目:https://zerojudge.tw/ShowProblem?problemid=a020
說明:在 Java 中沒有使用 Map,用 char array 來完成,Python 中使用 dictionary,相對 Java 來說,新增 dictionary 簡潔許多,// 運算符也令人感到特別。

Java 版
import java.util.Scanner;

public class A020 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int[] numb = new int[] {10,11,12,13,14,15,16,17,34,18,19,
                    20,21,22,35,23,24,25,26,27,28,29,32,30,31,33};
            char[] alpha = new char[] {'A','B','C','D','E','F',
                    'G','H','I','J','K','L','M','N','O','P',
                    'Q','R','S','T','U','V','W','X','Y','Z'};
            char [] ids = in.nextLine().toCharArray();
            int[] cal = new int [11];
            for(int i = 0; i < alpha.length; i++){
                if(ids[0] == alpha[i]){
                    cal[0] = numb[i] / 10;
                    cal[1] = numb[i] % 10;
                }
            }
            for(int i = 1; i < ids.length; i++)
                cal[i+1] = Integer.parseInt(String.valueOf(ids[i]));

            int sum = cal[0] + cal[10];
            for(int i = 1; i < 10; i++)
                sum += cal[i] * (10 - i);

            if(sum % 10 == 0)
                System.out.println("real");
            else
                System.out.println("fake");
        }
    }
}
Python 版 (2022.07)
alpha = {'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14,
         'F': 15, 'G': 16, 'H': 17, 'I': 34, 'J': 18,
         'K': 19, 'L': 20, 'M': 21, 'N': 22, 'O': 35,
         'P': 23, 'Q': 24, 'R': 25, 'S': 26, 'T': 27,
         'U': 28, 'V': 29, 'W': 32, 'X': 30, 'Y': 31, 'Z': 33}
ids = list(input())
s = alpha.get(ids[0]) // 10 + (alpha.get(ids[0]) % 10) * 9
s += int(ids[-1])
for i in range(1, 9):
    s += int(ids[-i - 1]) * i

if s % 10 == 0:
    print('real')
else:
    print('fake')

沒有留言:

張貼留言