Pat1031. Check ID card(15)

A legal ID number consists of 17-digit area, date number and sequence number plus a 1-digit check digit. The calculation rules of the 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 modulo 11 to get the value Z; finally, the value of Z and the check code M are corresponding 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 gives a positive integer N (<= 100) which is the number of ID numbers entered. Then N lines, each line gives an 18-digit ID number.

Output format:

Output 1 problematic ID number per line in the order entered. This does not check whether the first 17 digits are reasonable, only whether the first 17 digits are all numbers and the last 1 digit check code is calculated accurately. If all numbers are OK, output "All passed".

Input sample 1:
4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X
Sample output 1:
12010X198901011234
110108196711301866
37070419881216001X
Input sample 2:
2
320124198808240056
110108196711301862
Sample output 2:
All passed
//The first thought was to save it and calculate it together, but the result is wrong no matter what...
#include<iostream>
#include<string>
using namespace std;
int main() {
	int T;
	cin >> T;
	string str[101];//Save ID
	int sum[101] = { 0 };//Store the sum of the first 17 digits
	int Q[] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };//weighted
	char M[] = { '1','0','X','9','8','7','6','5','4','3','2' } ;//Check code
	bool flag[101];//Table judgment
	for (int i = 0; i<T; i++) {
		cin >> str[i];
		for (int j = 0; j<17; j++) {
			if (str[i][j] >= '0'&&str[i][j] <= '9')
				sum[i] += (str[i][j] - '0')*Q[j];
			else {
				flag[i] = false;
				break;
			}
		}if (flag[i]) {
			int Z = sum[i] % 11;
			if (str[i][17] != M[Z])
				flag[i] = false;
		}
	}
	//cout << endl;
	// determine the correct number
	int count = 0, x = 0, count1[101] = { 0 };//count
	for (int i = 0; i<T; i++) {
		if (flag[i])
			count++;
		else
			count1[x++] = i;
	}//Output the judgment result
	if (count == T)
		cout << "All passed" << endl;
	else {
		for (int i = 0; i<x; i++) {
			cout << str[count1[i]] << endl;
		}
	}
	return 0;
}



//Positive solution: calculate one by one
#include<iostream>
#include<string>
using namespace std;
int main() {
	int T;
	cin >> T;
	string str;//Save ID
	
	int Q[] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };//weighted
	char M[] = { '1','0','X','9','8','7','6','5','4','3','2' } ;//Check code
	int count = 0; // count
	for (int i = 0; i<T; i++) {
		cin >> str;
		int sum= 0;//Store the sum of the first 17 digits
		bool flag=true;//Table judgment
		for (int j = 0; j<17; j++) {
			if (str[j] >= '0'&&str[j] <= '9')
				sum += (str[j] - '0')*Q[j];
			else {
				flag = false;
				cout << str << endl;
				break;
			}
		}
			if (flag) {
				int Z = sum % 11;
			if (str[17] != M[Z]){
				flag = false;
				cout << str << endl;	
			}else
				count++;		
		}
	}
	// output the judgment result
	if (count == T)
		cout << "All passed" << endl;
	return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324718014&siteId=291194637