#10681: 真實身份證號碼沒問題,測資卻沒過


mickyshey (mickyshey)

學校 : 國立臺灣大學
編號 : 55929
來源 : [182.235.169.187]
最後登入時間 :
2016-06-01 21:00:37
a020. 身分證檢驗 | From: [1.169.60.103] | 發表日期 : 2016-02-05 23:33

#include<iostream>
#include<string>
using namespace std;
int main()
{
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int num[26] = {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};
string a;
int alpToNum = 0;
int sum = 0;
while (cin>>a)
{
for (int i = 0; i < 26; i++)
{
if (a[0] == alpha[i])
{
alpToNum = num[i];
break;
}
}
sum += (alpToNum % 10) * 9;
sum += (alpToNum / 10);
for (int i = 1; i < a.length() - 1; i++)
{
sum += ((9 - i)*((a[i] - 48)));
}
sum += (a[a.length() - 1] - 48);
if (sum % 10 == 0)
cout << "real" << endl;
else
cout << "fake" << endl;
}
return 0;
}

 

請各位大大幫忙解惑

 
#11135: Re:真實身份證號碼沒問題,測資卻沒過


uopsdod (samcom)

學校 : 不指定學校
編號 : 57407
來源 : [101.15.154.210]
最後登入時間 :
2019-07-14 16:59:15
a020. 身分證檢驗 | From: [1.161.195.191] | 發表日期 : 2016-07-07 06:19

#include
#include
using namespace std;
int main()
{
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int num[26] = {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};
string a;
int alpToNum = 0;
int sum = 0;
while (cin>>a)
{
for (int i = 0; i < 26; i++)
{
if (a[0] == alpha[i])
{
alpToNum = num[i];
break;
}
}
sum += (alpToNum % 10) * 9;
sum += (alpToNum / 10);
for (int i = 1; i < a.length() - 1; i++)
{
sum += ((9 - i)*((a[i] - 48)));
}
sum += (a[a.length() - 1] - 48);
if (sum % 10 == 0)
cout << "real" << endl;
else
cout << "fake" << endl;
}
return 0;
}

 

請各位大大幫忙解惑

 

在while裡面記得每一輪sum都要初始為零。

原本的code只要輸入一次錯誤的身分證字號,之後都會是錯的,因為sum一直累加上去,就無法被10整除。


while (cin>>a){

sum = 0;

...

...

...

}


 
ZeroJudge Forum