用C++编写一个程序,统计学生的成绩,并能包括输入学生的姓名和成绩。根据成绩从高到低排列,最后打印输出。
时间: 2023-07-15 22:15:28 浏览: 105
以下是一个简单的 C++ 程序,可以实现你所描述的功能:
```cpp
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
struct Student {
string name;
int score;
};
bool cmp(Student a, Student b) {
return a.score > b.score;
}
int main() {
int n;
cout << "请输入学生人数:";
cin >> n;
vector<Student> students(n);
for (int i = 0; i < n; i++) {
cout << "请输入第 " << i + 1 << " 个学生的姓名和成绩:";
cin >> students[i].name >> students[i].score;
}
sort(students.begin(), students.end(), cmp);
cout << "按成绩从高到低排名:" << endl;
for (int i = 0; i < n; i++) {
cout << "第 " << i + 1 << " 名:" << students[i].name << ",成绩:" << students[i].score << endl;
}
return 0;
}
```
程序中使用了 `struct` 定义了一个 `Student` 结构体,包含学生姓名和成绩两个成员变量。
程序首先读入学生人数,然后使用 `vector` 容器存储每个学生的信息。
接着,使用 `sort` 函数对学生信息按照成绩从高到低进行排序。
最后,按照排名顺序输出每个学生的姓名和成绩。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.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://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)