c++20分求助!!希望大佬帮帮我

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

```cpp for (int i = 0; i < n; i++) { if (stu[i].score > max) { max = stu.[i].score; maxIndex = i; } } ``` 要记得更新max
by silent_ST @ 2023-03-21 21:23:52


@[bella2010](/user/523585) 感谢,但是我刚提交依然20分,还是有问题@_@
by Aloteri @ 2023-03-21 21:26:26


@[Aloteri](/user/849659) accode ```cpp #include <iostream> using namespace std; const int N = 1001; int n; int mxtot, pos; struct stu { int c, m, e, tot; string name; }s[N]; int main() { mxtot = -1e5; cin >> n; for (int i = 1; i <= n; i ++ ) { cin >> s[i].name >> s[i].c >> s[i].m >> s[i].e; s[i].tot = s[i].c + s[i].m + s[i].e; if (s[i].tot > mxtot) { mxtot = s[i].tot; pos = i; } } cout << s[pos].name << " " << s[pos].c << " " << s[pos].m <<" " << s[pos].e << endl; } ```
by Geirangerfjard @ 2023-03-21 21:27:46


@[Aloteri](/user/849659) 更新max之后改一下stu的范围,因为有可能有极端数据,建议比范围多开一点
by Geirangerfjard @ 2023-03-21 21:31:23


@[Alone_Helpless](/user/823773) 接着建议score在输入时更新,不然可能有问题
by Geirangerfjard @ 2023-03-21 21:31:51


@[Aloteri](/user/849659) 改一下就过了 ```cpp #include<iostream> #include<string> using namespace std; struct Student { string m_Name; int c, m, e ,score; }stu[1010]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> stu[i].m_Name >> stu[i].c >> stu[i].m >> stu[i].e; stu[i].score = stu[i].c + stu[i].m + stu[i].e; } int max = stu[0].score, maxIndex = 0; for (int i = 0; i < n; i++) { if (stu[i].score > max) { maxIndex = i; max = stu[i].score; } } cout << stu[maxIndex].m_Name << " " << stu[maxIndex].c << " " << stu[maxIndex].m <<" "<< stu[maxIndex].e << endl; return 0; } ```
by Geirangerfjard @ 2023-03-21 21:32:23


@[Aloteri](/user/849659) 看一下有问题吗
by Geirangerfjard @ 2023-03-21 21:34:01


您的代码存在以下问题: 1. 在定义结构体时 ```cpp int c, m, e ,score=c+m+e; ``` $score$ 的值是不会计算的,要在读入后赋值 ```cpp for (int i = 0; i < n; i++) { stu[i].score = stu[i].c + stu[i].m + stu[i].e; } ``` 2. 在打擂台找最大值的时候 $max$ 的值没更新,也可以写成以下形式: ```cpp if (stu[i].score > stu[maxIndex].score) { maxIndex = i; } ``` 完整代码: ```cpp #include<iostream> #include<string> using namespace std; struct Student { string m_Name; int c, m, e ,score; }stu[1000]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> stu[i].m_Name >> stu[i].c >> stu[i].m >> stu[i].e; } for (int i = 0; i < n; i++) { stu[i].score = stu[i].c + stu[i].m + stu[i].e; } int max = stu[0].score, maxIndex = 0; for (int i = 1; i < n; i++) { if (stu[i].score > stu[maxIndex].score) { maxIndex = i; } } cout << stu[maxIndex].m_Name <<" " << stu[maxIndex].c <<" " << stu[maxIndex].m <<" "<< stu[maxIndex].e << endl; return 0; } ``` 最后,加油!
by HY248 @ 2023-03-21 21:34:18


~~回复好快~~ %%%%
by HY248 @ 2023-03-21 21:36:14


@[Alone_Helpless](/user/823773) 跪谢跪谢!!!!
by Aloteri @ 2023-03-21 21:36:48


| 下一页