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

    AC代码

    #include <cstdio>
    #include <iostream>
    using namespace std;
    const int max_n = 110;
    //权重
    int W[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'};
    int main() {
        #ifdef ONLINE_JUDGE
        #else
            freopen("1.txt", "r", stdin);
        #endif // ONLINE_JUDGE
        int n;
        char a;
        scanf("%d", &n);
    
        int num = 0; //问题身份证数量
        char err[max_n][18] = {0}; //存储问题身份证号
        for(int i = 0; i < n; i++) {
            bool flag = true;
            char temp[18] = {0};
            int sum = 0, t = 0; //t:计数正确的身份证号
            //printf("%d:", i);
            for(int j = 0; j < 18; j++) { //读取身份证号
                cin >> a;
                temp[j] = a;
            }
            for(int h = 0; h < 17; h++) {
                //printf("%c", temp[h]);
                if(!(temp[h] >= '0' && temp[h] <='9')){
                    //printf("break:%c
    ", temp[h]);
                    break;
                }
                int n = temp[h] - '0';
                //printf(" n: %d  ", n);
                sum += n * W[h];
                t++;
            }
    //        printf("
    ");
    //        printf("t:%d ", t);
            if(t < 17) flag = false;
            for(int h = 0; h < 18; h++) {
                err[i][h] = temp[h];
            }
            if(M[sum % 11] != temp[17]) flag = false;//验证码不等于身份证号最后一位
            //printf("flag = %d, sum = %d
    ", flag, sum);
            if(flag == 0) { //存储问题身份证号
                for(int j = 0; j < 18; j++) {
                    err[num][j] = temp[j];
                }
                num++; //问题身份证数量加一
            }
        }
        if(num == 0) {
            printf("All passed"); //身份证全部正确
        } else if(num != 0){
            //printf("%d
    ", num);
            for(int h = 0; h <= num - 1; h++) {
                for(int j = 0; j < 18; j++) {
                    printf("%c", err[h][j]);
                }
                printf("
    ");
            }
        }
        return 0;
    }
    
  • 相关阅读:
    springmvc log4j 配置
    intellij idea maven springmvc 环境搭建
    spring,property not found on type
    intellij idea maven 工程生成可执行的jar
    device eth0 does not seem to be present, delaying initialization
    macos ssh host配置及免密登陆
    centos7 搭建 docker 环境
    通过rest接口获取自增id (twitter snowflake算法)
    微信小程序开发体验
    gitbook 制作 beego 参考手册
  • 原文地址:https://www.cnblogs.com/isChenJY/p/11319536.html
Copyright © 2011-2022 走看看