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;
}
原文地址:https://www.cnblogs.com/isChenJY/p/11319536.html