现有 N 名同学参加了期末考试,并且获得了每名同学的信息:姓名(不超过 8 个字符的仅有英文小写字母的字符串)、语文、数学、英语成绩(均为不超过 150 的自然数)。总分最高的学生就是最厉害的,请输出最厉害的学生各项信息(姓名、各科成绩)。如果有多个总分相同的学生,输出靠前的那位。
第一行输入一个正整数 N,表示学生个数。
第二行开始,往下 N 行,对于每一行首先先输入一个字符串表示学生姓名,再输入三个自然数表示语文、数学、英语的成绩。均用空格相隔。
输出最厉害的学生。
3
senpai 114 51 4
lxl 114 10 23
fafa 51 42 60
senpai 114 51 4
数据保证,1≤N≤1000,姓名为长度不超过 8 的字符串,语文、数学、英语成绩均为不超过 150 的自然数。
代码捏:
#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;
}