題目連結 http://zerojudge.tw/ShowProblem?problemid=a054。此題是a020: 身分證檢驗變化題。因為檢查碼 c = 10 - m ,c 會有十種結果,並以同餘的算法,就可以建立好這十種結果的對照表,程式碼中的 prtResult 是可以換成陣列方式的。
程式碼:
#include <iostream>
using namespace std;
void prtResult(int n)
{
switch(n) {
case 10:
cout << "BNZ" << endl;
break;
case 1:
cout << "AMW" << endl;
break;
case 2:
cout << "KLY" << endl;
break;
case 3:
cout << "JVX" << endl;
break;
case 4:
cout << "HU" << endl;
break;
case 5:
cout << "GT" << endl;
break;
case 6:
cout << "FS" << endl;
break;
case 7:
cout << "ER" << endl;
break;
case 8:
cout << "DOQ" << endl;
break;
case 9:
cout << "CIP" << endl;
break;
default:
cout << "Error" << endl;
break;
}
}
int main() {
string num;
while(cin >> num)
{
int sum = 0;
for(unsigned int i = 0; i < num.size() - 1; i++)
{
sum += (num[i] - '0') * (8-i);
}
sum += (num[num.size() - 1]-'0');
int tar = 10 - (sum % 10);
prtResult(tar);
}
return 0;
}
沒有留言:
張貼留言