有5个学生,每个学生的数据包括学号(num) ,姓名(name) ,总成绩(score) ,编程实现从
时间: 2023-11-27 21:03:32 浏览: 138
这个题目给出的信息比较简单,我们可以使用结构体来存储每个学生的信息。然后,我们可以使用冒泡排序或者快速排序来对学生的总成绩进行排序。
以下是C++的代码示例:
```cpp
#include <iostream>
#include <string>
using namespace std;
// 定义一个结构体来表示学生信息
struct Student {
int num;
string name;
double score;
};
// 冒泡排序函数
void bubbleSort(Student arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j].score < arr[j+1].score) {
swap(arr[j], arr[j+1]);
}
}
}
}
int main() {
// 定义5个学生信息
Student stu[5] = {
{1001, "Tom", 85.5},
{1002, "Jerry", 76.5},
{1003, "Alice", 92.0},
{1004, "Bob", 80.0},
{1005, "Peter", 65.5}
};
// 对学生信息按照总成绩排序
bubbleSort(stu, 5);
// 输出排序后的学生信息
cout << "按总成绩排序后的学生信息为:" << endl;
for (int i = 0; i < 5; i++) {
cout << "学号:" << stu[i].num << ",姓名:" << stu[i].name << ",总成绩:" << stu[i].score << endl;
}
return 0;
}
```
输出结果为:
```
按总成绩排序后的学生信息为:
学号:1003,姓名:Alice,总成绩:92
学号:1001,姓名:Tom,总成绩:85.5
学号:1004,姓名:Bob,总成绩:80
学号:1002,姓名:Jerry,总成绩:76.5
学号:1005,姓名:Peter,总成绩:65.5
```