PRew_ @ 2023-11-06 19:38:11
现有
第一行输入一个正整数
第二行开始,往下
输出最厉害的学生。
3
senpai 114 51 4
lxl 114 10 23
fafa 51 42 60
senpai 114 51 4
数据保证,
代码捏:
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int chinese;
int math;
int english;
};
int main() {
int N;
cin >> N;
Student maxStudent;
int maxScore = 0;
for (int i = 0; i < N; i++ ) {
Student student;
cin >> student.name >> student.chinese >> student.math >> student.english;
int totalScore = student.chinese + student.math + student.english;
if (totalScore > maxScore) {
maxScore = totalScore;
maxStudent = student;
}
}
cout << maxStudent.name << " " << maxStudent.chinese << " " << maxStudent.math << " " << maxStudent.english << endl;
}
by Sicosuki @ 2023-11-06 19:45:58
学生的成绩有可能是0 0 0
把maxScore的初始值改成-1就可以了
by Sicosuki @ 2023-11-06 19:46:17
@nothing_exe_studio
by PRew_ @ 2023-11-06 19:50:55
谢谢dalao 已关注