PAT Basic 1031 Identification Card (15 points)

Keywords: PHP

A legitimate ID card number consists of 17-digit area, date number, sequence number and 1-digit check code. The calculation rules of the check codes are as follows:

Firstly, the weighted sum of the first 17 digits is allocated as {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; then the value Z is obtained by modularizing the calculated and 11 digits; finally, the value Z corresponds to the value of the check code M according 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

Given some ID number, please verify the validity of the check code and output the wrong number.

Input format:

The first line of input gives a positive integer N (=) which is the number of ID numbers entered. Then N lines, each line gives an 18-digit ID number.

Output format:

Output a problematic ID number per line in the order of input. It does not check whether the first 17 bits are reasonable or not. It only checks whether the first 17 bits are all numbers and the last 1 bit check code is calculated accurately. If all numbers are normal, output All passed.

Input Sample 1:

4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X

Output Sample 1:

12010X198901011234
110108196711301866
37070419881216001X

Input Sample 2:

2
320124198808240056
110108196711301862

Output Sample 2:

All passed

Thanks to Teacher Fan Jianzhong of Fuyang Normal University for Supplementary Data

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

 

#include<iostream>
using namespace std;
char vali(int z){
    switch(z){
        case 0:return '1';
        case 1:return '0';
        case 2:return 'X';
        case 3:return '9';
        case 4:return '8';
        case 5:return '7';
        case 6:return '6';
        case 7:return '5';
        case 8:return '4';
        case 9:return '3';
        case 10:return '2';
    }
}
int main() {
    int weight[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
    int T,sum;
    string tmp;
    char ch;
    bool isAllPass=true;
    cin>>T;
    for(int i=0;i<T;i++){
        sum=0;
        cin>>tmp;
        for(int i=0;i<tmp.length()-1;i++){
            sum+=((tmp[i]-'0')*weight[i]);
        }
        sum=sum%11;
        ch=vali(sum);
        if(!(ch==tmp[tmp.length()-1])){
            isAllPass=false;
            cout<<tmp<<endl;
        }
    }
    if(isAllPass){
        cout<<"All passed";
    }
    system("pause");
    return 0;
}

Posted by Ward on Thu, 10 Oct 2019 14:00:33 -0700