高中生程式解題系統:身分證檢驗

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

題目連結 http://zerojudge.tw/ShowProblem?problemid=a020

建立一個英文代號的對照表 :
short encode[] = {
    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
};

接著再依照題目說明的規則即可。

程式碼:

#include <iostream>
using namespace std;
int main()
{
short encode[] = {
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
};
const short SIZE = 8;
char a;
int n;
while( cin >> a >> n)
{
int temp = encode[a - 'A'];
int check = (temp % 10) * 9 + temp / 10;
check += (n % 10);
n = n / 10;
for(int i = 1; i <= SIZE; i++)
{
check += (n % 10) * i;
n = n / 10;
}
if( check % 10 == 0 )
cout << "real";
else
cout << "fake";
cout << endl;
}
return 0;
}
view raw a020.cpp hosted with ❤ by GitHub

沒有留言: