当前位置:网站首页>Pat class B 1031 checking ID card (15 points)

Pat class B 1031 checking ID card (15 points)

2022-06-21 07:22:00 myriadddddd

A legal ID number is from 17 Location 、 Date number and sequence number plus 1 Bit check code composition . Check code calculation rules are as follows :

First of all, to the front 17 Bit number weighted sum , The weight is assigned to :{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; Then the sum of the calculated values is compared to the 11 Take the modulus to get the value Z; Finally, according to the following relationship Z Value and check code 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 give me some ID number. , Please verify the validity of the check code , And output the problem number .

Input format :

Enter the first line to give a positive integer N(≤100) The number of the ID number entered. . And then N That's ok , Each line gives 1 individual 18 I. D. number .

Output format :

Output each line in the order of input 1 Question Id number . This is not before the test 17 Is it reasonable , Just before checking 17 Are all digits and last 1 Accurate calculation of bit check code . If all the numbers are OK , The output All passed.

sample input 1:

4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X

sample output 1:

12010X198901011234
110108196711301866
37070419881216001X

sample input 2:

2
320124198808240056
110108196711301862

sample output 2:

All passed

analysis :

First, accumulate the first 17 digits of the ID card , If the position is not a number, it means that this string is not an ID card ; Right. 18 Bit special judgment , If it is X Just use 10 Instead of , If not, add up the value . The final sum and remainder 11 And M Verify whether the check code in can correspond to the upper .

Code :

#include <iostream>

using namespace std;

//  The weight 
int weight[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
//  Check code 
int M[] = { 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 };

bool isTrue(string str) {
    int sum = 0;
    for (int i = 0; i != 17; i++) {
        if (str[i] < '0' || str[i] > '9')	// 0  ~ 17  Bits are all numbers 
            return false;
        sum += (str[i] - '0') * weight[i];	//  Calculate weight 
    }
    //  Judge whether the 18th digit is a letter 
    int temp = (str[17] == 'X') ? 10 : (str[17] - '0');
    return M[sum % 11] == temp;	//  Compare with the check code 
}

int main() {
    int N;
    string str;
    bool flag = false;
    cin >> N;
    for (int i = 0; i != N; i++) {
        cin >> str;
        if (!isTrue(str)) {
            cout << str << endl;
            flag = true;
        }
    }
    if (flag == 0)
        cout << "All passed" << endl;
    return 0;
}
原网站

版权声明
本文为[myriadddddd]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221515505145.html