身份證校正(c++實現)

來源:互聯網
上載者:User

標籤:

描述:

我國國標〖GB 11643-1999〗中規定:公民身份號碼是18位特徵組合碼,由十七位元字本體碼和一位元字校正碼組成。排列順序從左至右依次為:六位元字地址碼,八位元字出生日期碼,三位元字順序碼和一位元字校正碼。其校正碼(最後一位)計算方法和步驟為:(1)十七位元字本體碼加權求和公式S = Sum(Ai * Wi), i = 0, ... , 16 ,先對前17位元字的權求和其中Ai:表示第i位置上的社會安全號碼碼數字值Wi:表示第i位置上的加權因子,前17位加權因子從左至右分別為Wi:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2(2)計算模Y = mod(S, 11)(3)通過模Y查下表得到對應的校正碼
Y 0 1 2 3 4 5 6 7 8 9 10
校正碼 1 0 X 9 8 7 6 5 4 3 2
例如:某身份證前17位為11010519491231002
i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
  1 1 0 1 0 5 1 9 4 9 1 2 3 1 0 0 2
積 7 9 0 5 0 20 2 9 24 27 7 18 30 5 0 0 4
得到和為:167;則模為y=167%11=2查(3)得校正碼為X(大寫)請按上面所述步驟編程,輸入一個二代社會安全號碼,檢查該身份證是否正確。 輸入:輸入若干行,每行一個社會安全號碼碼,最後一行輸入-1輸出:輸出1代表正確,0代表錯誤input:
120223198902021249130132199210293822130402198207290622-1
output:
1
1
0
 1 #include<iostream> 2 #include<string> 3 using namespace std; 4  5 int cal(string a) 6 { 7     return (a[0] - ‘0‘) * 7 + (a[1] - ‘0‘) * 9 + (a[2] - ‘0‘) * 10 +  (a[3] - ‘0‘) * 5 + (a[4] - ‘0‘) * 8 + (a[5] - ‘0‘) *4  + (a[6] - ‘0‘) * 2 + (a[7] - ‘0‘) * 1 + (a[8] - ‘0‘) * 6 +(a[9] - ‘0‘) * 3 + (a[10] - ‘0‘) * 7 + (a[11] - ‘0‘) * 9 + (a[12] - ‘0‘) * 10 + (a[13] - ‘0‘) * 5 + (a[14] - ‘0‘) * 8 + (a[15] - ‘0‘) * 4 +(a[16] - ‘0‘) * 2; 8 } 9 10 char s(string a)11 {12     int k = cal(a) % 11;13     if (k == 0)14         return ‘1‘;15     else if (k == 1)16         return ‘0‘;17     else if (k == 2)18         return ‘X‘;19     else20         return ‘0‘+12-k;21 }22 int main()23 {24     string number;25     while ((cin >> number) && number != "-1")26     {27         if (number[17] == s(number))28             cout << "1" << endl;29         else30             cout << "0" << endl;31     }32     system("pause");33     return 0;34 }    

 

身份證校正(c++實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.