PAT甲级(Advanced Level) Practice 1028 List Sorting

原题

1028 List Sorting - PAT (Advanced Level) Practice

题目大意

输入n个学生的id、姓名、分数,再输入C表示对C列进行排序。

id:从小到大排

姓名:姓名不同时从小到大排,相同时id从小到大排

分数:不同时从小到大排,相同时id从小到大排

解题思路

调用自带的sort函数,自定义比较函数

代码(c++)
cpp 复制代码
#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

struct stu {
    string id, name;
    int score;
}stus[N];

bool cmp1(const stu &a, const stu &b) {              // 比较id
    return a.id < b.id;
}

bool cmp2(const stu &a, const stu &b) {              // 比较姓名
    if (a.name != b.name) return a.name < b.name;
    return a.id < b.id;
}

bool cmp3(const stu &a, const stu &b) {              // 比较分数
    if (a.score != b.score) return a.score < b.score;
    return a.id < b.id;
}

int main() {
    int n, cmp;
    cin >> n >> cmp;
    for (int i = 0; i < n; i++)
        cin >> stus[i].id >> stus[i].name >> stus[i].score;
    switch (cmp) {
        case 1:
            sort(stus, stus + n, cmp1);
            break;
        case 2:
            sort(stus, stus + n, cmp2);
            break;
        case 3:
            sort(stus, stus + n, cmp3);
            break;
        default:
            return 0;
    }
    for (int i = 0; i < n; i++)
        cout << stus[i].id << " " << stus[i].name << " " << stus[i].score << endl;

    return 0;
}
相关推荐
2601_9679801138 分钟前
Python中heapq模块的各种用法实例
数据结构·python·算法·应用实例·heapq模块
漂流瓶jz3 小时前
UVA-1615 高速公路 题解答案代码 算法竞赛入门经典第二版
数据结构·算法·贪心算法·贪心·aoapc·算法竞赛入门经典·uva
凌冰_13 小时前
数据结构 - 时间空间复杂度(一)
数据结构·算法·软件工程
Pniubi13 小时前
力扣2 . 两数相加
算法·leetcode·职场和发展
戴·菲瑜13 小时前
为什么PPO难以做出顶级棋类引擎
算法·博弈论·rl·ppo
在所不辞兄16 小时前
为什么数字孪生驱动的产线标定需要在线更新机制,而不能一次标定后长期沿用?
人工智能·神经网络·其他·算法·机器学习
AvocadoZeroOneOI20 小时前
B4500 [GESP202603 三级] 凯撒密码 详细解题报告
c++·算法
cjz38991 天前
双指针技巧:覆盖式双指针(原地覆盖)
算法
橘子131 天前
【高级数据库】关系代数基本概念
算法
电科_银尘2 天前
【大模型算法与工程 15】手写Self-Attention:Q、K、V与缩放点积
算法