文章

19

粉丝

0

获赞

1

访问

363

头像
身份证校验 题解:C++ 数组
P1722 北京理工大学机试题
发布于2025年6月23日 12:09
阅读数 28

#include<bits/stdc++.h>
using namespace std;

int main(){
	string s;
	while(cin>>s){
		int num[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
		int m[11]={1,0,10,9,8,7,6,5,4,3,2};
		long long sum=0;
		if(s.length()==18){
			for(int i=0;i<17;i++){
				sum=sum+(s[i]-'0')*num[i];
			}
			int x=sum%11;
			if(x==2){
				if(s[17]=='X'){
					cout<<"ID Corrent"<<endl;
				}else{
					cout<<"ID Wrong"<<endl;
				}
			}else{
				if((s[17]-'0')==m[x]){
					cout<<"ID Corrent"<<endl;
				}else{
					cout<<"ID Wrong"<<endl;
				}
			}
		}else{
			cout<<"ID Wrong"<<endl;
		}
	}
	return 0;
}

这里设置m数组为校验位,其中使用10来代替X;因为身份证中存在X特殊值,所以if判断中单独列出

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发