90分求助各位大神

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

S0soK1ng @ 2024-11-02 20:04:29

求助各位大神 第三个数据点 下载了数据测试结果多了一个问号在名字后面

#include<stdio.h>

int sum[1000] = { 0 };

struct student {
    char name[8];
    int chinese_score;
    int math_score;
    int english_score;
};

struct student students[1000];

int sum_score(int i) {
    int result = students[i].chinese_score 
        + students[i].math_score 
        + students[i].english_score;

    return result;
}

int main() {
    int n;
    scanf("%d", &n);

    int i;
    for (i = 0; i < n; i++) {
        scanf("%s", students[i].name);
        scanf("%d %d %d", &students[i].chinese_score, 
            &students[i].math_score, 
            &students[i].english_score);
        sum[i] = sum_score(i);
    }

    int max_scorer = 0;
    int max_score = sum[0];
    for (i = 1; i < n; i++) {
        if (sum[i] > max_score) {
            max_score = sum[i];
            max_scorer = i;
        }
    }

    printf("%s %d %d %d",students[max_scorer].name,
        students[max_scorer].chinese_score,
        students[max_scorer].math_score, 
        students[max_scorer].english_score);
    return 0;
}

by DM001 @ 2024-11-02 22:06:49

name数组改大一点就过了,至于为什么我也是来找答案的


by DM001 @ 2024-11-02 22:15:35

哦,我在网上搜到了,就是在C语言中用占位符%s时会自动在字符后面加一个结束符\0就会导致8大小的字符数组超了


|