问题描述 从1999年10月1日开始,公民身份证号码由15位数字增至18位。(18位身份证号码简介)。升级方法为: 1、把15位身份证号码中的年份由2位(7,8位)改为四位。 2、最后添加一位验证码。验证码的计算方案: 将前 17 位分别乘以对应系数 (7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2) 并相加,然后除以 11 取余数,0-10 分别对应 1 0 x 9 8 7 6 5 4 3 2。 请编写一个程序,用户输入15位身份证号码,程序生成18位身份证号码。假设所有要升级的身份证的四位年份都是19××年 输入格式 一个15位的数字串,作为身份证号码 输出格式 一个18位的字符串,作为升级后的身份证号码 样例输入 110105491231002 样例输出 11010519491231002x 数据规模和约定 不用判断输入的15位字符串是否合理
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 string a; 6 int b[20]; 7 cin>>a; 8 for(int i=0;i<6;i++) 9 { 10 b[i]=a[i]-'0'; 11 } 12 b[6]=1; 13 b[7]=9; 14 for(int i=6,j=8;i<15;i++,j++) 15 { 16 b[j]=a[i]-'0'; 17 } 18 19 int sum=b[0]*7+b[1]*9+b[2]*10+b[3]*5+b[4]*8+b[5]*4+b[6]*2+b[7]*1+b[8]*6+b[9]*3+b[10]*7+b[11]*9+b[12]*10+b[13]*5+b[14]*8+b[15]*4+b[16]*2; 20 int agv=sum%11; 21 int f; 22 char e; 23 switch(agv){ 24 case 0: {f=1; 25 26 } 27 case 1: 28 { 29 f=0;break; 30 } 31 // case 2: 32 case 3: {f=9; 33 break; 34 } 35 case 4: { 36 f=8; 37 break; 38 } 39 case 5: { 40 f=7; 41 break; 42 } 43 case 6: { 44 f=6; 45 break; 46 } 47 case 7: { 48 f=5; 49 break; 50 } 51 case 8: { 52 f=4; 53 break; 54 } 55 case 9: { 56 f=3; 57 break; 58 } 59 case 10: { 60 f=2; 61 break; 62 } 63 } 64 65 for(int i=0;i<17;i++) 66 { 67 cout<<b[i]; 68 } 69 if(agv==2) 70 { 71 cout<<"x"; 72 } 73 else cout<<f; 74 return 0; 75 }
我用的是比较笨的办法 还有一种办法是把那些余数写成一个数组存起来 这样赋值时候就简单一点