PAT(A)1028 List Sorting (25分)
程序员文章站
2022-07-08 22:20:00
...
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;
}