[PAT B1031] check ID

A valid area 17 by the ID number, order number, and date code plus a checksum component. Checksum calculation rule is as follows:
First, a weight is assigned to the first 17-bit digital weighted sum, right: {7,9,10,5,8,4,2,1,6,3,7,9,10,5 , 8,4,2}; and then the calculated value of 11 obtained modulo Z; Z value corresponding to the final value of the check code M in accordance with the following relationship:
Z: 0. 5. 4. 3. 1 2. 6. 7. 9 10. 8
M : 1 0 X 9 8 7 6 5 4 3 2
are now given number identification number, please verify the validity of check code, and outputs the number in question.
Input format:
input of the first row is given a positive integer N (<= 100) is the number of the input ID number. Then N rows, each row 18 is given an ID number.
Output format:
each line of output an ID number in question in the order of input. Here is not whether a reasonable inspection before 17, just before checking whether all numbers 17 and finally a checksum is calculated accurately. If all the numbers are normal, the output "All passed".
Input Sample. 1:
. 4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X
sample output. 1:
12010X198901011234
110108196711301866
37070419881216001X
Input Sample 2:
2
320124198808240056
110108196711301862
Output Sample 2:
All passed

#include <stdio.h>

int main() {
    int N;
    char id[20];
    bool allPassed = true;
    int weight[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
    char M[11] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
    scanf("%d", &N);

    for (int i = 0; i < N; i++) {
        scanf("%s", id);
        int sum = 0;
        for (int j = 0; j < 17; j++) {
            sum = sum + weight[j] * (id[j] - '0');
        }
        int z = sum % 11;
        if (M[z] != id[17]) {
            allPassed = false;
            printf("%s\n", id);
        }
    }
    if (allPassed)
        printf("ALL passed");

    return 0;
}

Test Results:
Here Insert Picture Description
Here Insert Picture Description

Published 33 original articles · won praise 1 · views 4133

Guess you like

Origin blog.csdn.net/qq_39827677/article/details/103934501