PAT 1031 查验身份证 (15 分)

2018-12-15  本文已影响0人  昭明ZMing
#include <stdio.h>
int main()
{
    int N;
    int weight[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
    char ZtoM[] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
    char ID[19];
    
    scanf("%d", &N);
    int d=0,sum=0,count = 0;     /* index, weighted sum and count for legal IDs */
    for(int i = 0; i < N; i++)
    {
        scanf("%s", ID);
        for(d=0,sum=0; d < 17 && ID[d] >= '0' && ID[d] <= '9'; d++)
            sum += (ID[d] - '0') * weight[d];
        if(d == 17 && ID[17] == ZtoM[sum % 11])     /* legal ID */
            count++;
        else                                       
            puts(ID); /* illegal ID */
    }
    if(count == N)
        puts("All passed");
    
    return 0;
}

GitHub

上一篇下一篇

猜你喜欢

热点阅读