题目链接
题目分析
按不同属性排序输出
解题思路
结构体排序,按要求使用不同参数cmp
即可
AC程序(C++)
/**************************
//@Author: 3stone
//@ACM: PAT-A1028 List Sorting
//@Time: 18/1/27
//@IDE: VS2017
***************************/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
typedef struct {
string id;
string name;
int grades;
}Stu;
bool cmp1(Stu s1, Stu s2) {
return s1.id < s2.id;
}
bool cmp2(Stu s1, Stu s2) {
if (s1.name != s2.name) return s1.name < s2.name;
else return s1.id < s2.id;
}
bool cmp3(Stu s1, Stu s2) {
if (s1.grades != s2.grades) return s1.grades < s2.grades;
else return s1.id < s2.id;
}
void mySort(Stu stu[], int n, int key) {
switch (key) {
case 1:
sort(stu, stu + n, cmp1);
break;
case 2:
sort(stu, stu + n, cmp2);
break;
case 3:
sort(stu, stu + n, cmp3);
}
}
Stu stu[100010];
int main() {
int n, key;
char id[10], name[10];
scanf("%d%d", &n, &key);
for (int i = 0; i < n; i++) {
scanf("%s %s %d", id, name, &stu[i].grades);
stu[i].id = id;
stu[i].name = name;
}
mySort(stu, n, key); //按要求排序
for (int i = 0; i < n; i++) {
printf("%s %s %d\n", stu[i].id.c_str(), stu[i].name.c_str(), stu[i].grades);
}
system("pause");
return 0;
}