PAT (Basic Level) Practice (Chinese) 1031 ID verification (15 points)

A legal ID card number consists of 17 digits of region, date number and sequence number plus 1 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 take the calculated sum modulo 11 to get the value Z; finally, correspond to the Z value and 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

Now given some ID numbers, please verify the validity of the check code and output the number in question.

Input format:

Input the first line to give a positive integer N (≤100) is the number of input ID numbers. Then there are N lines, and each line gives a 18-digit ID card number.

Output format:

Output 1 problematic ID card number per line in the order of input. It does not check whether the first 17 digits are reasonable, but only checks whether the first 17 digits are all numbers and the last 1 digit 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
! ! ! ! ! ! ! Here to explain to you what is weighted summation?

Weighting: the product of each ID number and the weight of the corresponding position . For example, ID number 320124198808240056, the weight distribution is: {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}, the first weighting Just 3*7 , the second time 2*9 , and so on.

The weighted summation is of course the sum of all weighted numbers (such as 3 * 7+2 * 9+.....+) , here it should be noted that after the weighted summation of the first 17 digits of the ID card, the modulus of 11 is taken ( remainder) to judge whether it is equal to the 18th check code.

C++:
#include<iostream>
using namespace std;
int main()
{
    int a[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};   //题目所给的权重分配
    char c[]={'1','0','X','9','8','7','6','5','4','3','2'};//权重求和取模后所对应的校验码
    int n;
    cin>>n;
    int sum=0;
    int judge=0; //用来判断所给身份证是否有问题 0表示为没有问题 1表示为有问题
    string s;
    for(int i=1;i<=n;i++)
    {
        cin>>s;   //这里没有涉及输入空格所以用cin直接输入即可
        sum=0;    //权重之和
        for(int j=0;j<17;j++)  //遍历身份证前十七位数字
        {
            if(s[j]=='X')    //如果出现非数字即身份证有问题
            {
                cout<<s<<endl;  //直接输出
                judge=1;      //然后判断标记为1(有问题)
                break;
            }
                
            else     
            {
                sum+=( (s[j]-'0') *a[j]);    //否则的话就加权这个数字
                
            }
                
        }
        if(c[sum%11]!=s[17])    //判断加权求和取模后对应的校验码是否等于最后一位
        {
            cout<<s<<endl;     //如果不是即身份证有问题,输出即可
            judge=1;           //判断标记
        }
        
            
    }
    if(judge==0)   //所有输入完成后若标记还是为0即所有身份证都没有问题
            cout<<"All passed";   //输出All passed
}

This problem can also be solved using two-dimensional arrays and counting methods (it is recommended to think about a problem from multiple angles and directions)

C language:
#include<stdio.h>
int main()
{
    int n,sum,count=0,judge;             //count表示身份证有问题的数量
    int a[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
    char c[]={'1','0','X','9','8','7','6','5','4','3','2'};
    char num[101][101];     //二维字符数组来输入
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",&num[i]);
    }
    for(int i=1;i<=n;i++)
    {
        sum=0;           //权重之和
        judge=0;
        for(int k=0;k<17;k++)
        {
            if(num[i][k]=='X') 
                judge=1;         //判断身份证是否有问题  1为有问题  0为没有问题
            else
                sum+=(num[i][k] - '0') * a[k];    //加每个符合条件加权后的数
        }
        if(c[sum%11] != num[i][17])    //判断加权求和取模后对应的校验码是否等于最后一位
            judge=1;
        if(judge == 1)      
        {
            printf("%s\n",num[i]);
            count++;              //每输出一串有问题的身份证号码count就自增1
        }
     } 
     if(count == 0)            //count为0即没有有问题的身份证号码
         printf("All passed");
    return 0;
    
}

Guess you like

Origin blog.csdn.net/H1727548/article/details/128885087