算法提高 身份证号码升级

/*
算法提高 身份证号码升级  

问题描述
  从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位字符串是否合理

*/

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        StringBuffer str1 = new StringBuffer(sc.nextLine());
        str1.insert(6, "19");
        sc.close();
        int r=
        Integer.parseInt(str1.charAt(0)+"")*7+
        Integer.parseInt(str1.charAt(1)+"")*9+
        Integer.parseInt(str1.charAt(2)+"")*10+
        Integer.parseInt(str1.charAt(3)+"")*5+
        Integer.parseInt(str1.charAt(4)+"")*8+
        Integer.parseInt(str1.charAt(5)+"")*4+        
        Integer.parseInt(str1.charAt(6)+"")*2+
        Integer.parseInt(str1.charAt(7)+"")*1+
        Integer.parseInt(str1.charAt(8)+"")*6+
        Integer.parseInt(str1.charAt(9)+"")*3+        
        Integer.parseInt(str1.charAt(10)+"")*7+
        Integer.parseInt(str1.charAt(11)+"")*9+
        Integer.parseInt(str1.charAt(12)+"")*10+
        Integer.parseInt(str1.charAt(13)+"")*5+
        Integer.parseInt(str1.charAt(14)+"")*8+
        Integer.parseInt(str1.charAt(15)+"")*4+
        Integer.parseInt(str1.charAt(16)+"")*2;
        int rr = r % 11;
        String t = "";
        switch (rr) {
        case 0:
            t = "1";
            break;
        case 1:
            t = "0";
            break;
        case 2:
            t = "x";
            break;
        default:
            t = String.valueOf(12 - rr);
        }
        System.out.print(str1.toString() + t);

    }
}

 

posted @ 2018-03-22 15:13  忧伤的小毛驴  阅读(209)  评论(0编辑  收藏  举报