7-63 查驗身份證 (15 分)
阿新 • • 發佈:2019-02-05
7-63 查驗身份證 (15 分)
一個合法的身份證號碼由17位地區、日期編號和順序編號加1位校驗碼組成。校驗碼的計算規則如下:
首先對前17位數字加權求和,權重分配為:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然後將計算的和對11取模得到值Z
;最後按照以下關係對應Z
值與校驗碼M
的值:
Z:0 1 2 3 4 5 6 7 8 9 10
M:1 0 X 9 8 7 6 5 4 3 2
現在給定一些身份證號碼,請你驗證校驗碼的有效性,並輸出有問題的號碼。
輸入格式:
輸入第一行給出正整數N(≤100)是輸入的身份證號碼的個數。隨後N行,每行給出1個18位身份證號碼。
輸出格式:
按照輸入的順序每行輸出1個有問題的身份證號碼。這裡並不檢驗前17位是否合理,只檢查前17位是否全為數字且最後1位校驗碼計算準確。如果所有號碼都正常,則輸出All passed
。
輸入樣例1:
4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X
輸出樣例1:
12010X198901011234
110108196711301866
37070419881216001X
輸入樣例2:
2
320124198808240056
110108196711301862
輸出樣例2:
All passed
#include <stdio.h> #define ID_LEN 18 //可能gets輸入和scanf輸入編碼不同吧,它不認gets輸入,求教大神 int main () { char id[ID_LEN+1],m[]={'1','0','X','9','8','7','6','5','4','3','2'}; int i,result=1,cnt,weight[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; scanf ("%d",&cnt); while(cnt--){ int z=0; fflush(stdin); //清除緩衝區 gets(id); for( i=0; i<17; i++){ if ( id[i]>='0' && id[i]<= '9' ) {//檢查前17位是否為數字 z += (id[i] - '0')*weight[i]; } else { goto out; } } z %= 11; if (id[17] != m[z]) { //檢查校驗碼 out: //前17不為數字錯誤資訊輸出 result = 0; puts(id); } } if (result == 1) printf("All passed\n"); return 0 ; }
#include <stdio.h> #define ID_LEN 18 //可以通過 int main () { char id[ID_LEN+1],m[]={'1','0','X','9','8','7','6','5','4','3','2'}; int i,result=1,cnt,weight[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; scanf ("%d",&cnt); while(cnt--){ int z=0; scanf ("%s",id); for( i=0; i<17; i++){ z += (id[i] - '0')*weight[i]; } z %= 11; if (id[17] != m[z]) { //檢查校驗碼 result = 0; printf("%s\n",id); } } if (result) printf("All passed\n"); return 0 ; }