文章

2

粉丝

11

获赞

0

访问

693

头像
身份证校验 题解:
P1722 北京理工大学2017年机试题
发布于2024年7月27日 11:11
阅读数 309

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

int main(){
	 map<int,char> dict;//余数x和校验位y的对应规则
	 dict[0]='1';//一定要用单引号
	 dict[1]='0';
	 dict[2]='X';//注意X是大写
	 dict[3]='9';
	 dict[4]='8';
	 dict[5]='7';
	 dict[6]='6';
	 dict[7]='5';
	 dict[8]='4';
	 dict[9]='3';
	 dict[10]='2';
	 int weight[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
	 string id;//C++的 string 类对于字符串操作很方便,但是输入输出只能用 cin、cout
	 while(cin>>id){
		 if(id.length()!=18){
		 	cout<<"ID Wrong"<<endl;
		 }else{
			 int temp=0;
			 for(int i=0;i<=16;i++){
				 //temp+=id[i]*weight[i];
				 temp+=(id[i]-'1'+1)*weight[i];
				 temp=temp%11;
			 }
		 	if(id[17]==dict[temp]){
				cout<<"ID Corrent"<<endl;
			}else{
				cout<<"ID Wrong"<<endl;
			}
		 }
	 }
	 return 0;
 }

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发