PTA check ID card JAVA

Posted by psyickphuk on Sat, 16 Nov 2019 18:14:52 +0100

Check ID card

A legal ID card number consists of 17 digit area, date number and sequence number plus 1 digit verification code. The calculation rules of check code are as follows:

First, the first 17 digits are weighted and summed, and the weight distribution is: {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; then the calculated sum is modeled to get the value Z; finally, the value Z and the check code M are corresponding according to the following relationship:

Z: 0 1 2 3 4 5 6 7 8 9 10
M: 1 0 X 9 8 7 6 5 4 3 2

Now given some ID numbers, please verify the validity of the verification code and output the number in question.

Input format:

The first line of input shows that the positive integer N (≤ 100) is the number of ID number input. Then line N, each line gives an 18 digit ID number.

Output format:

Output 1 problematic ID card number per line in the order of input. It does not check whether the first 17 digits are reasonable. It only checks whether the first 17 digits are all numbers and the last one digit check code is accurate. If all numbers are normal, All passed is output.

Enter example 1:

4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X

Output example 1:

12010X198901011234
110108196711301866
37070419881216001X

Enter example 2:

2
320124198808240056
110108196711301862

Output example 2:

All passed

import	java.util.*;
public class Main {

	public static void main(String[] args) {
		int n, i;
		Main work = new Main();
		Scanner input = new Scanner(System.in);
		n = input.nextInt();
		String[] number = new String[n];
		for(i=0;i<n;i++)
		{
			number[i] = input.next();
		}
		boolean flag = true;
		for(i = 0;i<n;i++)
		{
			if(work.Check_number(number[i]))
			{				
				}
			else
			{
				flag = false;
				System.out.println(number[i]);
			}
		}
		if(flag)
		{
			System.out.println("All passed");
		}
		
	}
	public	boolean Check_number(String number)
	{
		int[] right = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
		char[] code = {'1','0','X','9','8','7','6','5','4','3','2'};
		int all,i,order,z;
		char temp,check;
		all = 0;
		for(i = 0;i<17;i++)
		{
			temp = number.charAt(i);
			order = temp - '0';
			all = all + order*right[i];
		}
		z = all%11;
		check = number.charAt(17);
		if(check == code[z])
			return true;
		else
			return false;
	}
}

Topics: Java