一个号码由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
输出样例1:
12010X198901011234
110108196711301866
37070419881216001X
输入样例2:
2
320124198808240056
110108196711301862
输出样例2:
All passed
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int lines = sc.nextInt();
int[] id = new int[17];
int[] qz = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
String[] xy = new String[] { "1", "0", "X", " 9", " 8", "7", " 6", " 5", " 4", " 3", " 2" };
boolean isFlag = true;
JUMP: for (int i = 0; i < lines; i++) {
String s = sc.next();
String[] ids = s.split("");
for (int k = 0; k < 17; k++) {
try {
id[k] = Integer.parseInt(ids[k]);
} catch (NumberFormatException e) {
System.out.println(s);
isFlag = false;
continue JUMP;
}
id[k] *= qz[k];
}
String x = ids[17];
int sum = 0;
for (int j = 0; j < 17; j++) {
sum += id[j];
}
int z = sum % 11;
if (z == 0 && x.equals("1")) {
continue;
} else if (z == 1 && x.equals("0")) {
continue;
} else if (z == 2 && x.equals("X")) {
continue;
} else if (z == 3 && x.equals("9")) {
continue;
} else if (z == 4 && x.equals("8")) {
continue;
} else if (z == 5 && x.equals("7")) {
continue;
} else if (z == 6 && x.equals("6")) {
continue;
} else if (z == 7 && x.equals("5")) {
continue;
} else if (z == 8 && x.equals("4")) {
continue;
} else if (z == 9 && x.equals("3")) {
continue;
} else if (z == 10 && x.equals("2")) {
continue;
} else {
System.out.println(s);
isFlag = false;
}
}
if (isFlag) {
System.out.println("All passed");
}
sc.close();
}
}