给定N个学生的基本信息,包括学号(由5个数字组成的字符串)、姓名(长度小于10的不包含空白字符的非空字符串)和3门课程的成绩([0,100]区间内的整数),要求输出总分最高学生的姓名、学号和总分。
时间: 2023-12-08 21:03:12 浏览: 23
可以使用结构体来存储每个学生的信息,然后遍历结构体数组求出总分最高学生的信息。
以下是一个示例代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
struct Student {
string id; // 学号
string name; // 姓名
int score[3]; // 3门课程的成绩
int totalScore; // 总分
};
int main() {
int n;
cin >> n;
Student students[n];
for (int i = 0; i < n; i++) {
cin >> students[i].id >> students[i].name;
students[i].totalScore = 0;
for (int j = 0; j < 3; j++) {
cin >> students[i].score[j];
students[i].totalScore += students[i].score[j];
}
}
int maxScore = 0;
int maxIndex = 0;
for (int i = 0; i < n; i++) {
if (students[i].totalScore > maxScore) {
maxScore = students[i].totalScore;
maxIndex = i;
}
}
cout << students[maxIndex].name << " " << students[maxIndex].id << " " << students[maxIndex].totalScore << endl;
return 0;
}
```
这个程序会先读入学生的数量 `n`,然后使用一个结构体数组 `students` 存储每个学生的信息。在读入每个学生的信息时,会计算出该学生的总分并存储在结构体的 `totalScore` 字段中。
然后程序遍历结构体数组,找到总分最高的学生,并输出该学生的姓名、学号和总分。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)