错误提示为N=100, 包含所有11种映射,是什么意思?谢谢
本帖最后由 NYJYA 于 2019-3-9 11:58 编辑一个合法的身份证号码由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<cstring>
#include<cmath>
using namespace std;
int main()
{
char a,m;
int N,i,j,c,d = 0;
long z;
cin >> N;
getchar();
for(i = 0;i < N;i++)
for(i = 0;i < N;i++)
{
z = 0;
cin >> a;
/* for(j = 0;j < strlen(a) - 1;j++)
{
if(a < '0' && a > '9')
{
cout << a << endl;
break;
}
}
if(a < '0' && a > '9') continue;*/
for(c = 0;c < strlen(a) -1;c++)
{
if(c == 0||c == 10) z+=(a-'0')*7;
if(c == 1 || c == 11) z += (a-'0')*9;
if(c == 2 || c == 12) z += (a-'0')*10;
if(c == 3 || c == 13) z += (a-'0')*5;
if(c == 4 || c == 14) z += (a-'0')*8;
if(c == 5 || c == 15) z += (a-'0')*4;
if(c == 6 || c == 16) z += (a-'0')*2;
if(c == 7) z += (a-'0')*1;
if(c == 8) z += (a-'0')*6;
if(c == 9) z += (a-'0')*3;
}
z = z % 11;
switch(z)
{
case 0:if(a != '0')
{
cout << a << endl;
}
else d++;
break;
case 1:if(a != '1')
{
cout << a << endl;
} else d++;
break;
case 2:if(a != 'x')
{
cout << a << endl;
} break;
case 3:if(a != '9')
{
cout << a << endl;
}
else d++; break;
case 4:if(a != '8')
{
cout << a << endl;
}else d++;
break;
case 5:if(a != '7')
{
cout << a << endl;
}else d++;
break;
case 6:if(a != '6')
{
cout << a << endl;
}else d++;
break;
case 7:if(a != '5')
{
cout << a << endl;
}else d++;
break;
case 8:if(a != '4')
{
cout << a << endl;
}else d++;
break;
case 9:if(a != '3')
{
cout << a << endl;
}else d++;
break;
case 10:if(a != '2')
{
cout << a <<endl;
}else d++;
break;
}
}
if(d == N) cout << "All passed" << endl;
return 0;
} 话说你这写法。。学了数组就用起来啊,别一串if,看着头大,自己也不好改
#include <iostream>
using namespace std;
const int weight[] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };
const char M[] = { '1','0','X','9','8','7','6','5','4','3','2' };
bool islegal(char* nums) {
int Z = 0;
for (int i = 0; i < 17; ++i) {
Z += weight * (nums - '0');
}
Z %= 11;
return (M == nums);
}
int main() {
int N;
char nums;
cout << "请输出需要检验的身份证号码的数目:";
cin >> N;
cout << "需要检验的身份证号码的数目为:" << N << "\n\n请输入需要检验的身份证号:\n";
for (int i = 0; i < N; ++i) {
cin >> nums;
}
bool Allpassed = true;
for (int i = 0; i < N; ++i) {
if (!islegal(nums)) {
if (Allpassed) {
Allpassed = false;
cout << "有问题的身份证号为:\n";
}
cout << nums << endl;
}
}
if (Allpassed) {
cout << "All passed!" << endl;
}
system("pause");
}
页:
[1]