https://www.patest.cn/contests/pat-b-practise/1031
题目描述:
一个合法的身份证号码由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 <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <ctype.h>
using namespace std;
int main()
{
int n = 0 , i = 0, sum = 0, count =0 , num=0;
string id = "";
int w [17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2} ;
char check[11] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
cin >> n;
num = n;
while ( n--)
{
sum = 0;
cin >> id ;
if ( id.length() != 18)
{
cout << id << endl;
continue ;
}
for ( i = 0; i< 17 ; i++)
{
if (!(id[i]>='0' && id[i] <= '9'))
{
cout << id << endl;
break;
}
sum += (id[i]-'0')*w[i] ;
}
if ( i < 17) continue ;
sum %= 11 ;
if (check[sum] == id[17]) count ++ ;
else cout << id << endl;
}
if (count == num) cout <<"All passed\n" ;
return 0;
}
本文介绍了一种身份证号码校验码的有效性验证方法,包括校验码的计算规则及其实现代码。通过分析身份证号码的结构,利用特定权重进行求和并按规则计算校验码,最终验证输入身份证号码的有效性。
2958

被折叠的 条评论
为什么被折叠?



