标题:身份证最后一位怎么确认 求路过大神指点
只看楼主
Ever0123
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2020-11-12
结帖率:100%
已结贴  问题点数:20 回复次数:2 
身份证最后一位怎么确认 求路过大神指点
问题是根据输入的身份证号码确认最后一位数字对不对;
#include<stdio.h>
#include<string.h>
int main()
{
    char st[20];
    int T, i, j , sum = 0;
    scanf("%d", &T);
    while (T--)
    {
        sum = 0;
        scanf("%s", st);
        int len;
        len = strlen(st);
        if (len == 18)
        {
            for (i = 0; i < 17; i++)
            {
                switch (i)
                {
                case 0:sum += (st[i] - '0') * 7; break;
                case 1:sum += (st[i] - '0') * 9; break;
                case 2:sum += (st[i] - '0') * 10; break;
                case 3:sum += (st[i] - '0') * 5; break;
                case 4:sum += (st[i] - '0') * 8; break;
                case 5:sum += (st[i] - '0') * 4; break;
                case 6:sum += (st[i] - '0') * 2; break;
                case 7:sum += (st[i] - '0') * 1; break;
                case 8:sum += (st[i] - '0') * 6; break;
                case 9:sum += (st[i] - '0') * 3; break;
                case 10:sum += (st[i] - '0') * 7; break;
                case 11:sum += (st[i] - '0') * 9; break;
                case 12:sum += (st[i] - '0') * 10; break;
                case 13:sum += (st[i] - '0') * 5; break;
                case 14:sum += (st[i] - '0') * 8; break;
                case 15:sum += (st[i] - '0') * 4; break;
                case 16:sum += (st[i] - '0') * 2; break;
                }
            }
            int yu;
            yu = sum % 11;
            char re;
            switch (yu)
            {
            case 0:re = '1'; break;
            case 1:re = '0'; break;
            case 2:re = 'X'; break;
            case 3:re = '9'; break;
            case 4:re = '8'; break;
            case 5:re = '7'; break;
            case 6:re = '6'; break;
            case 7:re = '5'; break;
            case 8:re = '4'; break;
            case 9:re = '3'; break;
            case 10:re = '2'; break;
            }
            if (re == st[17])
                printf("right\n");
            else
                printf("wrong\n");
 
        }
    }
    return 0;
}
这个代码在oj上可以通过,但是在电脑上不会运行
搜索更多相关主题的帖子: break sum 过大 case int 
2020-11-26 11:00
纯蓝之刃
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:76
帖 子:554
专家分:3690
注 册:2019-7-29
得分:10 

一沙一世界,一花一天堂。无限掌中置,刹那成永恒。
2020-11-26 15:51
风过无痕1989
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:8
帖 子:228
专家分:1050
注 册:2020-7-17
得分:10 
回复 楼主 Ever0123
程序代码:
#include "stdio.h"

int main(int argc, char* argv[])
{
    int i, T, sum = 0;
    
    char a[18];    //int a[18];//身份证的18位数字
    int b[17] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };    //前17位数的系数
    char c[11] = { 'f' };    //存放校验结果
    
    printf("请输入身份证号码:");
    for (i = 0;i < 18;i++)
    {
        scanf("%c", &a[i]);
    }

    for (i = 0;i < 17;i++)    // sum = sum + a[i] * b[i]
    {
        sum += (a[i] - '0') * b[i];
    }
    
    T = sum % 11;
    switch (T)
    {
    case 0: c[0] = '1';break;
    case 1: c[1] = '0';break;
    case 2: c[2] = 'X';break;
    case 3: c[3] = '9';break;
    case 4: c[4] = '8';break;
    case 5: c[5] = '7';break;
    case 6: c[6] = '6';break;
    case 7: c[7] = '5';break;
    case 8: c[8] = '4';break;
    case 9: c[9] = '3';break;
    case 10: c[10] = '2';break;
    }

    if (c[T] == a[17])
    {
        printf("输入身份证为真\n");
    }
    else
    {
        printf("输入身份证为假\n");
    }

}
2020-11-27 15:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-504011-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.727984 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved