zoukankan      html  css  js  c++  java
  • PAT 1031. 查验身份证(15)

    一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下:

    首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然后将计算的和对11取模得到值Z;最后按照以下关系对应Z值与校验码M的值:

    Z:0 1 2 3 4 5 6 7 8 9 10
    M:1 0 X 9 8 7 6 5 4 3 2

    现在给定一些身份证号码,请你验证校验码的有效性,并输出有问题的号码。

    输入格式:

    输入第一行给出正整数N(<= 100)是输入的身份证号码的个数。随后N行,每行给出1个18位身份证号码。

    输出格式:

    按照输入的顺序每行输出1个有问题的身份证号码。这里并不检验前17位是否合理,只检查前17位是否全为数字且最后1位校验码计算准确。如果所有号码都正常,则输出“All passed”。

    输入样例1:

    4
    320124198808240056
    12010X198901011234
    110108196711301866
    37070419881216001X
    

    输出样例1:

    12010X198901011234
    110108196711301866
    37070419881216001X
    

    输入样例2:

    2
    320124198808240056
    110108196711301862
    

    输出样例2:

    All passed
    
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 int main(){
     5     int n;
     6     scanf("%d",&n);
     7     getchar(); 
     8     int a[20]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
     9     char b[20]={'1','0','X','9','8','7','6','5','4','3','2'};
    10     int flag = 0;
    11     char temp[20];
    12     int sum = 0;
    13     int i,j;
    14     while(n--){
    15         scanf("%s",temp);
    16         getchar();
    17         sum = 0;
    18         for(i=0;i<17;i++){
    19             if(temp[i]<'0'||temp[i]>'9'){
    20                 flag = 1;
    21                 puts(temp);
    22                 break;
    23             }
    24                 
    25         }
    26         if(i==17){
    27             for(j=0;j<i;j++){
    28                 sum = sum+(temp[j]-'0')*a[j];
    29             }
    30             if(b[sum%11]!=temp[17]){
    31                 puts(temp);
    32                 flag = 1;
    33             }
    34                 
    35         }
    36     }
    37     if(flag==0)
    38         printf("All passed");
    39 }
  • 相关阅读:
    SVN服务器搭建(一)
    排序算法二:冒泡排序
    【LeetCode】136. Single Number
    【LeetCode】217. Contains Duplicate
    【LeetCode】189. Rotate Array
    【LeetCode】122. Best Time to Buy and Sell Stock II
    【LeetCode】26. Remove Duplicates from Sorted Array
    【LeetCode】20. Valid Parentheses
    【LeetCode】680. Valid Palindrome II
    【LeetCode】345. Reverse Vowels of a String
  • 原文地址:https://www.cnblogs.com/lolybj/p/6213364.html
Copyright © 2011-2022 走看看