所以我这有啥问题

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

lzagddsg @ 2022-08-07 20:54:23

我的代码:

#include <bits/stdc++.h>

using namespace std;

struct student
{
    string name;
    int ch, ma, en, id, sum;
} stu[10000], m;
int n;
int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> stu[i].name >> stu[i].ch >> stu[i].ma >> stu[i].en;
        stu[i].sum = stu[i].ch + stu[i].ma + stu[i].en;
        stu[i].id = i;
        if (m.sum < stu[i].sum)
            m = stu[i];
        else if (m.sum == stu[i].sum)
            if (m.id > stu[i].id)
                m = stu[i];
    }
    cout << m.name << " " << m.ch << " " << m.ma << " " << m.en;
    return 0;
}

结果第一个错了。请各位大佬指教。


by Always_Remember_It @ 2022-08-07 21:12:15

@lzagddsg 初始化m.sum=-1;


by Always_Remember_It @ 2022-08-07 21:14:26

@lzagddsg 有可能所有学生都0分。。。(很坑)


by lzagddsg @ 2022-08-08 15:40:40

@zswmb 哦!!!


|