PAT(A)1028 List Sorting (25分)

在这里插入图片描述

Sample Input

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

Sample Output

000001 Zoe 60
000007 James 85
000010 Amy 90

思路:
三种不同的输入代表三种不同的排序,如果相同则根据id排序。
代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>

using namespace std;

#define endl '\n'

typedef long long ll;

struct node {
    string id;
    string name;
    int score;
}peo[100005];

bool cmp1(node a, node b)
{
    return a.id < b.id;
}

bool cmp2(node a, node b)
{
    if (a.name != b.name)
        return a.name < b.name;
    return a.id < b.id;
}

bool cmp3(node a, node b)
{
    if (a.score != b.score)
        return a.score < b.score;
    return a.id < b.id;
}

int main()
{
    int n, m;

    cin >> n >> m;

    for (int i = 0; i < n; ++i)
    {
        cin >> peo[i].id;
        cin >> peo[i].name;
        cin >> peo[i].score;
    }

    if (m == 1)
        sort(peo, peo + n, cmp1);
    if (m == 2)
        sort(peo, peo + n, cmp2);
    if (m == 3)
        sort(peo, peo + n, cmp3);

    for (int i = 0; i < n; ++i)
        cout << peo[i].id << " " << peo[i].name << " " << peo[i].score << endl;

    return 0;
}

发布了161 篇原创文章 · 获赞 7 · 访问量 7092

猜你喜欢

转载自blog.csdn.net/weixin_43778744/article/details/104083392