可以请教一下dl们个问题吗?
查看原帖
可以请教一下dl们个问题吗?
593001
Langrange2021楼主2022/9/17 18:20

这是我第一遍没有过的代码,用了循环遍历字符数组赋值,然而没有过。

#include <stdio.h>
#include <string.h>
struct test
{
    char name[9];
    int chin;
    int math;
    int eng;
};
int main()
{
    int max = -1;
    int N;
    scanf("%d", &N);
    char na[9];
    int ch = 0;
    int ma = 0;
    int en = 0;
    for (int k = 0; k < N; k++)
    {
        struct test store;
        int sum;
        scanf("%s", store.name);
        scanf("%d%d%d", &store.chin, &store.math, &store.eng);
        sum = store.chin + store.math + store.eng;
        if (sum > max)
        {
            max = sum;
            for (int q = 0; q < 9; q++)
            {
                if (store.name[q] == '\0')
                {
                    break;
                }
                na[q] = store.name[q];
            }
            ch = store.chin;
            ma = store.math;
            en = store.eng;
        }
    }
    for (int y = 0; y < 9; y++)
    {
        if (na[y] == '\0')
        {
            break;
        }
        printf("%c", na[y]);
    }
    printf(" ");
    printf("%d %d %d", ch, ma, en);
    return 0;
}

这是我第二遍用c语言strcpy函数改对的代码,ac全过

#include <stdio.h>
#include <string.h>
struct test
{
    char name[9];
    int chin;
    int math;
    int eng;
};
int main()
{
    int max = -1;
    int N;
    scanf("%d", &N);
    char na[9];
    int ch = 0;
    int ma = 0;
    int en = 0;
    for (int k = 0; k < N; k++)
    {
        struct test store;
        int sum;
        scanf("%s", store.name);
        scanf("%d%d%d", &store.chin, &store.math, &store.eng);
        sum = store.chin + store.math + store.eng;
        if (sum > max)
        {
            max = sum;
            strcpy(na, store.name);
            ch = store.chin;
            ma = store.math;
            en = store.eng;
        }
    }
    
    printf("%s ", na);
    printf("%d %d %d", ch, ma, en);
    return 0;
}

有没有dl可以解释一下循环遍历数组为什么不对吗?

2022/9/17 18:20
加载中...