有趣的地方

有趣的地方

二维数组传参练习题

二维数组传参,外部函数要接收指针数组

题目:

1031 查验身份证 - PAT (Basic Level) Practice (中文) (pintia.cn)

#include "head.h"

/*数组指针,指针指向数组,该数组为字符型数组,每个数组中有18个元素*/
int GetIdCards(char (*pidcards)[20], int maxlen)
{
    int n = 0;
    int i = 0;
    
    scanf("%d", &n);
    getchar();
    for (i = 0; i < n; ++i)
    {
        gets(pidcards[i]);
    }

    return n;
}

int IsCorrectIdCard(char *idcard)
{
    int i = 0;
    int sum = 0;
    int val[17] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
    char mode[11] = {'1','0','X','9','8','7','6','5','4','3','2'};

    for (i = 0; i < 17; ++i)
    {
        if (idcard[i] < '0' || idcard[i] > '9')
        {
            return 0;
        }
    } 

    for (i = 0; i < 17; ++i)
    {
        sum += (idcard[i] - '0') * val[i];
    }

    if (idcard[17] == mode[sum % 11])
    {
        return 1;
    }else
    {
        return 0;
    }
}

int main(void)
{
    char idcards[100][20] = {0};
    int len = 0;
    int i = 0;
    int allright = 1;

    len = GetIdCards(idcards, 100);
    for (i = 0; i < len; ++i)
    {
        if (!IsCorrectIdCard(idcards[i]))
        {
            allright = 0;
            printf("%s\n", idcards[i]);
        }
    }

    if (allright)
    {
        printf("All Passed!\n");
    }

    return 0;
}

重点在于如何处理二维数组

发表评论:

Powered By Z-BlogPHP 1.7.3

© 2018-2020 有趣的地方 粤ICP备18140861号-1 网站地图