身份证最后一位是根据前面十七位数字码,按照ISO 7064:1983.MOD 11-2校验码计算出
来的检验码。其计算方法是:
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 。
样例第一行为一个整数,表示测试样例数目,接下来每一行为一个测试样例:
2
53010219980508011X
340524200001010012
输出:
The check code is Right!
The check code is Wrong
代码如下:
#include <iostream>
using namespace std;
int main()
{
int xs[] = {7,9 ,10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
char dz[] = {'1', '0', 'X', '9' ,'8' ,'7', '6', '5', '4', '3', '2'};
int i,j,n,s,t;
char buf[20]={0};
cin >> n;
for (i=0;i<n;i++)
{
cin >> buf;
s=0;
for (j=0;j<17;j++)
{
s += (buf[j] - '0')*xs[j];
}
t = s%11;
if(buf[17] == dz[t])
cout << "The check code is Right!"<<endl;
else
cout <<"The check code is Wrong"<<endl;
}
return 0;
}
将系数搞一个数组,然后对输入的字符串前17个字符进行逐个相乘求和
和求余11,在将身份证号码对应尾数整成一个数组,用余数获取尾号进行对比就行了
#include <stdio.h>
int main()
{
int k[17] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char m[12] = "10X98765432";
char s[20];
int n,i,sum=0;
gets(s);
n = strlen(s);
if(n == 18)
{
for(i=0;i<17;i++)
sum += k[i]*(s[i]-'0');
if(s[17] == m[sum%11])
printf("The check code is Right!");
else
printf("The check code is Wrong");
}
return 0;
}