首页 > 试题广场 >

1031. 查验身份证(15)

[编程题]1031. 查验身份证(15)
  • 热度指数:17603 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下:
首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然后将计算的和对11取模得
到值Z;最后按照以下关系对应Z值与校验码M的值:
Z:0 1 2 3 4 5 6 7 8 9 10
M:1 0 X 9 8 7 6 5 4 3 2
现在给定一些身份证号码,请你验证校验码的有效性,并输出有问题的号码。

输入描述:
输入第一行给出正整数N(<= 100)是输入的身份证号码的个数。随后N行,每行给出1个18位身份证号码。


输出描述:
按照输入的顺序每行输出1个有问题的身份证号码。这里并不检验前17位是否合理,只检查前17位是否全为数字且最后1位校验码计算准确。如果所有号码都正常,
则输出“All passed”。
示例1

输入

4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X

输出

12010X198901011234
110108196711301866
37070419881216001X
 
public class IDCard {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        List<String> error = new ArrayList<>();
        int count = 0;
        for (int i = 0; i < N; i++) {
            String id = scan.next();
            if (!checkId(id)){
                error.add(id);
                count++;
            }
        }
        if (count==0){
            System.out.println("All passed");
        }else {
            for (int i = 0; i < count; i++) {
                System.out.println(error.get(i));
            }

        }

    }
    public static boolean checkId(String id){
        int wight [] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
        char M [] = {'1','0','X','9','8','7','6','5','4','3','2'};
        boolean flag = false;
        String regex = "\\d{17}\\w|\\d";
        int sum = 0 ;
        for (int i = 0; i < id.length()-1; i++) {
            char c = id.charAt(i);
            if (id.matches(regex)) {
                sum += (c-'0') * wight[i];
                flag = M[sum % 11] == id.charAt(17);
            } else {
                flag = false;
            }
        }
        return flag;
    }
}

发表于 2020-08-23 17:31:33 回复(0)
import java.util.Scanner;
public class Main{
 public static int cha(String p) {
  int[] weight = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
  int sum = 0;
  for (int i = 0; i < weight.length; i++) {
   sum += (weight[i] * (p.charAt(i) - '0'));
  }
  return sum % 11;
 }
 public static void main(String[] args) {
  Scanner in = new Scanner(System.in);
  int n = in.nextInt();
  String[] nn = new String[n];
  for (int i = 0; i < n; i++) {
   nn[i] = in.next();
  }
  int k = 0;
  int f = 0;
  String g = "10X98765432";
  for (int i = 0; i < n; i++) {
   for (int j = 0; j < 17; j++) {
    if (nn[i].charAt(j) <= '9' && nn[i].charAt(j) >= '0') {
     f++;
    } else {
     System.out.println(nn[i]);
     k++;
     break;
    }
   }
   if (f == 17) {
    if (nn[i].charAt(17) != g.charAt(cha(nn[i]))) {
     System.out.println(nn[i]);
     k++;
    }
   }
   f = 0;
  }
  if (k == 0) {
   System.out.println("All passed");
  }
 }
}

发表于 2019-03-12 21:48:18 回复(0)
//小白第一步,终于成功了,激动到想哭~.~
import java.util.*; public class Main{     private static final int[] W={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};     private static final char[] M = {'1','0','X','9','8','7','6','5','4','3','2'};     public static void main(String[] args){         Scanner sc = new Scanner(System.in);                  int N=sc.nextInt();         int count=0;         ArrayList<String> errors=new ArrayList<String>();         for(int i=0;i<N;i++){             String id=sc.next();             if (!checkId(id)){                 count+=1;                 errors.add(id);             }         }         if(count != 0){             for(int i=0;i<errors.size();i++){                 System.out.println(errors.get(i));             }         }         else{             System.out.print("All passed");         }     }     public static boolean checkId(String id){         int sum=0;         for (int j=0;j<17;j++){             if((id.charAt(j)>='0') && (id.charAt(j)<='9')){                 sum+=W[j]*(id.charAt(j)-'0');             }             else{                 return false;             }         }         return M[sum%11]==id.charAt(17);     } }
# coding=utf-8
#小白的Python版本,开心>.<
W=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
M=['1','0','x','9','8','7','6','5','4','3','2']
errors=[]
n=int(raw_input())
def Main(n,W,M):
for i in range(n):
id =raw_input()
if not checkId(id,W,M):
errors.append(id)
if len(errors)==0 :
print "All passed"
else:
for i in errors:
print i
def checkId(id,W,M):
sum=0
id_part=id[:17]
for i,c in enumerate(id_part):
if (c>='0') and (c <='9'):
#print c
sum+=W[i]*int(id_part[i])
else: 
return False
return M[sum%11]==id[-1]
Main(n,W,M)

编辑于 2018-05-09 15:07:16 回复(0)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
static Map<Integer,Integer> maps=new HashMap<Integer,Integer>();
public static void main(String[] args) {
	maps.put(0, 7);maps.put(1, 9);maps.put(2, 10);maps.put(3, 5);
	maps.put(4, 8);maps.put(5, 4);maps.put(6, 2);maps.put(7, 1);
	maps.put(8, 6);maps.put(9, 3);maps.put(10, 7);maps.put(11, 9);
	maps.put(12, 10);maps.put(13, 5);maps.put(14, 8);maps.put(15, 4);maps.put(16, 2);
	Map<Integer,Character> m=new HashMap<Integer,Character>();
	m.put(0, '1');m.put(1,'0');m.put(2,'x');m.put(3, '9');
	m.put(4, '8');m.put(5, '7');m.put(6, '6');m.put(7, '5');
	m.put(8, '4');m.put(9, '3');m.put(10, '2');
	Scanner input=new Scanner(System.in);
	int num=input.nextInt();
	String arr[]=new String[num];
	int saf[]=new int[num];
	for(int i=0;i<num;i++){
		arr[i]=input.next();
		saf[i]=calcu(arr[i].substring(0,17));
	}
	int count=0;
	for(int i=0;i<num;i++){
		if(saf[i]<0 || saf[i]>10){
			System.out.println(arr[i]);
			continue;
		}
		if(m.get(saf[i])!=arr[i].charAt(17)){
			System.out.println(arr[i]);
			count++;
		}
	}
	if(count==0)
		System.out.println("All passed");
}
private static int calcu(String sub) {
	int sum=0;
	int x=0;
	for(int i=0;i<sub.length();i++)
	{
		x=maps.get(i);
		sum=sum+(sub.charAt(i)-48)*x;	
	}
	sum=sum%11;
	return sum;
}
}

发表于 2017-08-31 23:33:26 回复(0)
import java.util.*;
public class Main {
	static int[] weight = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
	static char[] M = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			String[] arr = new String[n];
			for (int i = 0; i < n; i ++ )
				arr[i] = sc.next();
			boolean isFinded = false;
			for (int i = 0; i < n; i ++ ) {
				String s = arr[i];
				if(s.substring(0, 17).contains("X")) {
					isFinded = true;
					System.out.println(s);
				} else {
					int sum = 0;
					for (int j = 0; j < 17; j ++ )
						sum += (s.charAt(j) - '0') * weight[j];
					if(s.charAt(17) != M[sum % 11]) {
						isFinded = true;
						System.out.println(s);
					}
				}
			}
			if( ! isFinded) System.out.println("All passed");
		}
	}
}

发表于 2016-10-20 15:49:01 回复(0)