PAT Class B 1031 ID ID 15 (points)

topic

A legal ID number is composed of 17-digit area, date number and sequence number plus a check code. 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 obtained value and the calculated modulo 11 Z ; according to the relation corresponding to the final Z value and checksum M value:

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:

Enter the first line to give a positive integer NN N ( ≤ 1 0 0 \le 100 1 0 0 ) is the number of ID numbers entered. Then NN N lines, each line gives an 18-digit ID number.

Output format:

Output 1 problematic ID number per line in the order of input. It is not checked whether the first 17 digits are reasonable, but only whether the first 17 digits are all numbers and the check code of the last digit is calculated accurately. If all numbers are normal, then output All passed .

Input example 1:

4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X

Output sample 1:

12010X198901011234
110108196711301866
37070419881216001X

Input example 2:

2
320124198808240056
110108196711301862

Output sample 2:

All passed

Thanks to Fan Jianzhong, Fuyang Teachers College, for additional data

Thanks to Shi Xifan, teacher of Zhijiang College, Zhejiang University of Technology, for correcting the data

Code


#include<iostream>
using namespace std;
int main()
{
    
    
	int i,N, a[17] = {
    
     7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 },sum;
	char func[11] = {
    
    '1','0','X', '9', '8', '7', '6', '5', '4', '3','2'};
	string input;
	bool flag = true,in=false;
	cin >> N;
	while (N--)
	{
    
    
		in = false;
		sum = 0;
		cin >> input;
		for (i = 0; i<17; i++)
		{
    
    
			if (input[i] >= '0' && input[i] <= '9')
			{
    
    
				sum += a[i] * int(input[i]-48);
			}
			else
			{
    
    
				in = true;
				break;
			}
		}
		if(in||func[sum%11]!=input[17])
		{
    
     
			flag = false;
			cout << input<<endl;
		}
	}
	if (flag)
		cout << "All passed";
	return 0;
}

Question details link

Guess you like

Origin blog.csdn.net/qq_41985293/article/details/114983504