ZJANS

a020. 身分證檢驗

Easy Last Update: 2025/05/16
基本運算

給一身份證字號
驗證他存不存在

※ 不能直接對 ASCII 碼做運算,字母對應的數字沒有按順序排


解法一

評分結果(參考) : AC (2ms, 340KB)

#include <bits/stdc++.h>
using namespace std;

int letter2num(char c){
    switch(c){
        case 'A': return 1+0*9;        case 'N': return 2+2*9;
        case 'B': return 1+1*9;        case 'O': return 3+5*9;
        case 'C': return 1+2*9;        case 'P': return 2+3*9;
        case 'D': return 1+3*9;        case 'Q': return 2+4*9;
        case 'E': return 1+4*9;        case 'R': return 2+5*9;
        case 'F': return 1+5*9;        case 'S': return 2+6*9;
        case 'G': return 1+6*9;        case 'T': return 2+7*9;
        case 'H': return 1+7*9;        case 'U': return 2+8*9;
        case 'I': return 3+4*9;        case 'V': return 2+9*9;
        case 'J': return 1+8*9;        case 'W': return 3+2*9;
        case 'K': return 1+9*9;        case 'X': return 3+0*9;
        case 'L': return 2+0*9;        case 'Y': return 3+1*9;
        case 'M': return 2+1*9;        case 'Z': return 3+3*9;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    char in[10] = {0};

    cin >> in;
    int x = 0;
    
    for(int i=1; i<9; i++){
        x += (in[i]-'0') * (9-i);
    }
    x += letter2num(in[0]);
    x += in[9] - '0';
    
    if(x%10 == 0) cout << "real";
    else cout << "fake";

    return 0;
}
或者用陣列

評分結果(參考) : AC (4ms, 352KB)

#include <bits/stdc++.h>
using namespace std;

int letter[] = {
    1, 10, 19, 28, 37, 46, 55, 64, 39, // A~I
    73, 82, 2, 11, 20, 48, 29, 38, 47, // J~R
    56, 65, 74, 83, 21, 3, 12, 30 //S~Z
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    char in[11];
    cin >> in;
    
    int x = 0;
    for(int i=1; i<9; i++){
        x += (in[i]-'0') * (9-i);
    }
    x += letter[in[0]-'A'];
    x += in[9] - '0';
    
    if(x%10 == 0) cout << "real";
    else cout << "fake";

    return 0;
}