【GESP】C++五级真题(结构体排序考点) luogu-B3968 [GESP202403 五级] 成绩排序

在解决成绩排序问题时,尤其是针对 **GESP202403 五级** 的题目要求,需要实现多级排序规则,并且保留原始输入顺序以处理并列排名的情况。可以使用结构体(或类)来封装学生的成绩信息,并通过自定义比较函数实现多级排序逻辑。 ### 数据结构设计 定义一个 `Student` 结构体,用于存储每位学生的成绩信息以及计算出的总分: ```cpp struct Student { int math; // 数学成绩 int chinese; // 语文成绩 int english; // 英语成绩 int total; // 总分 int index; // 输入顺序(用于保留原始索引) // 构造函数 Student(int m, int c, int e, int idx) : math(m), chinese(c), english(e), index(idx) { total = m + c + e; } }; ``` ### 多级排序规则 排序的优先级如下: 1. **总分降序**(总分高的排在前面) 2. **语文与数学成绩之和降序** (总分相同时,语文+数学成绩高的优先) 3. **语文与数学中的最高分降序** (若语数之和也相同,则取语文和数学中的最高分进行比较) 4. **原始输入顺序升序** (若以上条件都相同,则按照输入顺序排序) 对应的比较函数如下: ```cpp bool cmp(const Student& a, const Student& b) { if (a.total != b.total) { return a.total > b.total; } int a_cm = a.chinese + a.math; int b_cm = b.chinese + b.math; if (a_cm != b_cm) { return a_cm > b_cm; } int a_max = max(a.chinese, a.math); int b_max = max(b.chinese, b.math); if (a_max != b_max) { return a_max > b_max; } return a.index < b.index; } ``` ### 完整程序示例 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Student { int math; int chinese; int english; int total; int index; Student(int m, int c, int e, int idx) : math(m), chinese(c), english(e), index(idx) { total = m + c + e; } }; bool cmp(const Student& a, const Student& b) { if (a.total != b.total) { return a.total > b.total; } int a_cm = a.chinese + a.math; int b_cm = b.chinese + b.math; if (a_cm != b_cm) { return a_cm > b_cm; } int a_max = max(a.chinese, a.math); int b_max = max(b.chinese, b.math); if (a_max != b_max) { return a_max > b_max; } return a.index < b.index; } int main() { int n; cin >> n; vector<Student> students; for (int i = 0; i < n; ++i) { int m, c, e; cin >> m >> c >> e; students.emplace_back(m, c, e, i); } sort(students.begin(), students.end(), cmp); for (const auto& s : students) { cout << s.math << " " << s.chinese << " " << s.english << endl; } return 0; } ``` ### 程序说明 - 使用 `vector<Student>` 存储所有学生对象。 -排序前,每个学生对象都记录了原始输入顺序 `index`。 - 使用 `sort` 函数配合自定义比较函数 `cmp` 完成多级排序- 输出按排序后的顺序输出各科成绩。 该算法的时间复杂度为 **O(n log n)**,适用于中等规模的数据排序需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值