题目大意:给出N个学生的id、name、grade信息,根据输入选择排序的尺度进行排序并输出。
按要求排序即可,使用函数指针可以稍微缩短代码长度。
AC代码:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
struct student
{
char id[10];
char name[10];
int grade;
}list[100010];
bool cmp1(student a, student b)
{
return strcmp(a.id, b.id) < 0;
}
bool cmp2(student a, student b)
{
return strcmp(a.name, b.name) != 0 ? strcmp(a.name, b.name) < 0 : strcmp(a.id, b.id) < 0;
}
bool cmp3(student a, student b)
{
return a.grade != b.grade ? a.grade < b.grade : strcmp(a.id, b.id) < 0;
}
bool (*cmp[3])(student a, student b) = {cmp1, cmp2, cmp3};
int main()
{
int N, C;
scanf("%d%d", &N, &C);
for (int i = 0; i < N; ++i)
scanf("%s%s%d", list[i].id, list[i].name, &list[i].grade);
sort(list, list + N, *(cmp[C-1]));
for (int i = 0; i < N; ++i)
printf("%s %s %d\n", list[i].id, list[i].name, list[i].grade);
return 0;
}