身份证号码的校验的计算方法:
1、将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7、9、10、5、8、4、2、1、6、3、7、9、10、5、8、4、2;
2、将这17位数字和系数相乘的结果相加;
3、用加出来和除以11,看余数是多少;
4、余数只可能有0、1、2、3、4、5、6、7、8、9、10这11个数字。其分别对应的最后一位身份证的号码为1、0、X、9、8、7、6、5、4、3、2,其中的X是罗马数字10;
5、通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的X;如果余数是10,身份证的最后一位号码就是2
法1:
#include<stdio.h>int ID_card(int* arr,int len)
{int brr[18] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,0 };int sum = 0;int i = 0;for (i; i < 18; i++){sum = sum + arr[i] * brr[i];}int a = sum % 11;switch (a){case 0:if (arr[17] != 1){printf("error\n");}else{printf("success\n");}break;case 1:if (arr[17] != 0){printf("error\n");}else{printf("success\n");}break;case 2:if (arr[17] != 'x'){printf("error\n");}else{printf("success\n");}break;case 3:if (arr[17] != 9){printf("error\n");}else{printf("success\n");}break;case 4:if (arr[17] != 8){printf("error\n");}else{printf("success\n");}break;case 5:if (arr[17] != 7){printf("error\n");}else{printf("success\n");}break;case 6:if (arr[17] != 6){printf("error\n");}else{printf("success\n");}break;case 7:if (arr[17] != 5){printf("error\n");}else{printf("success\n");}break;case 8:if (arr[17] != 4){printf("error\n");}else{printf("success\n");}break;case 9:if (arr[17] != 3){printf("error\n");}else{printf("success\n");}break;case 10:if (arr[17] != 2){printf("error\n");}else{printf("success\n");}break;}return a;
}int main()
{int arr[18] = { 3,1,0,3,2,3,1,9,9,9,0,8,3,7,5,0,5,2 };int len = sizeof(arr) / sizeof(arr[0]);ID_card(arr,len);return 0;
}
法2:
int ID_card()
{char crr[20];scanf_s("%s", &crr, 20);int tmp=0;int arr[]= { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,0 };for (int i = 0; i < 17; i++){tmp += (crr[i] - '0') * arr[i];}int flg = tmp % 11;char drr[] = {'1','0','x','9','8','7','6','5','4','3','2',};return crr[17] = drr[flg];}int main()
{bool tag = ID_card();if (tag){printf("身份证号码存在\n");}else{printf("身份证号码不存在\n");}return 0;
}