题目描述 从键盘输入若干个学生的信息,每个学生信息包括学号、姓名、3门课的成绩,计算每个学生的总分,输出总分最高的学生的信息。 输入 首先输入一个整数n(1<=n<=100),表示学生人数,然后输入n行,每行包含一个学生的信息:学号(12位)、姓名(不含空格且不超过20位),以及三个整数,表示语文、数学、英语三门课成绩,数据之间用空格隔开。 输出 输出总成绩最高的学生的学号、姓名、及三门课成绩,用空格隔开。若有多个最高分,只输出第一个。 样例输入 Copy 3 541207010188 Zhangling 89 78 95 541207010189 Wangli 85 87 99 541207010190 Fangfang 85 68 76 样例输出 Copy 541207010189 Wangli 85 87 99
时间: 2024-03-31 14:38:44 浏览: 21
这是一道经典的成绩统计问题,可以使用结构体来存储每个学生的信息,然后计算每个学生的总分,最后找到总分最高的学生输出其信息即可。
以下是 C++ 代码实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
struct Student {
string id;
string name;
int score[3];
int total_score;
};
int main() {
int n;
cin >> n;
Student max_student;
max_student.total_score = -1; // 初始化为-1,表示还没有找到最高分的学生
for (int i = 0; i < n; i++) {
Student s;
cin >> s.id >> s.name >> s.score[0] >> s.score[1] >> s.score[2];
s.total_score = s.score[0] + s.score[1] + s.score[2];
if (s.total_score > max_student.total_score) { // 更新最高分的学生
max_student = s;
}
}
cout << max_student.id << " " << max_student.name << " ";
for (int i = 0; i < 3; i++) {
cout << max_student.score[i] << " ";
}
cout << endl;
return 0;
}
```
输入格式为:
```
3
541207010188 Zhangling 89 78 95
541207010189 Wangli 85 87 99
541207010190 Fangfang 85 68 76
```
输出格式为:
```
541207010189 Wangli 85 87 99
```