给定N个学生的基本信息,包括学号(由5个数字组成的字符串)、姓名(长度小于10的不包含空白字符的非空字符串)和3门课程的成绩([0,100]区间内的整数),要求输出总分最高学生的姓名、学号和总分。
输入格式:
输入在一行中给出正整数N(≤10)。随后N行,每行给出一位学生的信息,格式为“学号 姓名 成绩1 成绩2 成绩3”,中间以空格分隔。
输出格式:
在一行中输出总分最高学生的姓名、学号和总分,间隔一个空格。题目保证这样的学生是唯一的。
输入样例:
5
00001 huanglan 78 83 75
00002 wanghai 76 80 77
00003 shenqiang 87 83 76
10001 zhangfeng 92 88 78
21987 zhangmeng 80 82 75
输出样例:
zhangfeng 10001 258
AC代码:
#include <bits/stdc++.h>
using namespace std;
struct Student {
string id;
string name;
int score1,score2,score3;
};
int main(){
int n;
cin >> n;
vector<Student> studentList(n);
for(int i=0; i<n; i++){
cin >> studentList[i].id >> studentList[i].name >> studentList[i].score1 >> studentList[i].score2 >> studentList[i].score3;
}
//直接找变量记录下来最大值
int maxScore = -1;
string maxId,maxName;
for(Student &student : studentList){
int totalScore = student.score1+student.score2+student.score3;
if(totalScore > maxScore){
maxScore = totalScore;
maxId = student.id;
maxName = student.name;
}
}
cout << maxName << " " << maxId << " " << maxScore << endl;
return 0;
}