求助

P5740 【深基7.例9】最厉害的学生

Kelly_1 @ 2024-02-29 21:33:00

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
struct Stu
{
    char num[8];
    int chese;
    int math;
    int elglish;
} stu[1000];
int main()
{
    int n;
    scanf("%d", &n);
    int i = 0;
    for (i = 0;i < n;i++)
    {
        scanf("%s %d %d %d", &stu[i].num, &stu[i].chese, &stu[i].math, &stu[i].elglish);
    }
    int j = 0;
    int max = 0;
    for (i = 0;i < n;i++)
    {
        int sum = stu[i].chese + stu[i].math + stu[i].elglish;
        if (max < sum)
        {
            max = sum;
            j = i;
        }
    }
    printf("%s %d %d %d", stu[j].num, stu[j].chese, stu[j].math, stu[j].elglish);
    return 0;
}

第三个没过,是哪里出错了吗


by 杜都督 @ 2024-02-29 21:43:55

结构体中第一行修改为char num[8+1];

因为字符串最后一位是'\0',也需要分配1个单位的空间

顺便,非常好单词拼写,使我的大脑旋转


by 杜都督 @ 2024-02-29 21:44:44

@Kelly_1


by Kelly_1 @ 2024-03-01 20:04:45

@杜都督 谢谢


|