PAT Basic Level 1031 查验身份证 (15 分)

本文深入探讨了身份证号码的有效性验证算法,通过多种C++实现方式对比,详细讲解了校验码计算流程及常见错误检查。包括数字合法性检验、校验码匹配等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:

https://pintia.cn/problem-sets/994805260223102976/problems/994805290334011392

我的未AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
using namespace std;


int main() {
	int n;
	vector<string> str(100);
	vector<bool> str_(100, false);
	char M[] = { '1','0','X','9','8','7','6','5','4','3','2' };
	int quan[] = { 7,9,0,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };
	while (cin >> n) {
		for (int i = 0; i < n; i++)
		{
			cin >> str[i];
			int sum = 0;
			for (int j = 0; j < 16; j++)
				sum += quan[j] * str[i][j];
			sum %= 11;
			if (M[sum] == str[i][16])
				str_[i] = true;
		}
		//检验前17位是否都是数字
		for (int i = 0; i < n; i++) {
			for (int j = 0; j <= 16; j++) {
				if (str[i][j] > '9' || str[i][j] < '0') {
					str_[i] = false;
					break;
				}
			}
		}
		int cnt = 0;
		for (int i = 0; i < n; i++) {
			if (!str_[i])
			{
				cout << str[i] << endl;
				cnt++;
			}
		}
		if (cnt == 0)
			cout << "All passed"<<endl;
	}
}

未AC代码(这题有问题):

#include <iostream>
#include <cstdio>

using namespace std;

int w[20]={ 7,9,0,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };

char change[15]={'1','0','X','9','8','7','6','5','4','3','2'};

int main(){
    int N;
    bool flag=true;//记录所有的身份证是否合法
    scanf("%d",&N);
    char str[20];
    for(int i=0;i<N;i++){
        int j,last=0;//记录校验码
        scanf("%s",str);
        for(j=0;j<17;j++){
            if(str[j]>'9'||str[j]<'0')
                break;
            last=(str[j]-'0')*w[j]+last;
        }
        if(j<17)
        {
            flag=false;
            printf("%s\n",str);
        }
        else{
            if(change[last%11]!=str[17])//身份证校验错误
            {
                flag=false;
                printf("%s\n",str);
            }
        }
    }
    if(flag==true){
        printf("All passed\n");
    }
    return 0;
}

二刷:

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int quanzhi[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char M[11]={'1','0','X','9','8','7','6','5','4','3','2'};
int main(){
    int n;
    int flag=true;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        string str;
        cin>>str;
        int sum=0,j;
        for(j=0;j<17;j++){
            if(!(str[j]>='0'&&str[j]<='9'))
                break;
            sum+=(str[j]-'0')*quanzhi[j];
        }
        if(j<17){
            cout<<str<<endl;
            flag=false;
        }
        else{//此处不能用if,否则会无效身份证会输出两次
            sum=sum%11;
            if(M[sum]!=str[17]){
                cout<<str<<endl;
                flag=false;
            }
        }
    }
    if(flag==true){
        printf("All passed\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值