知识点
-
这题的数据比较大,得用scanf,而且要换全都换,不然还是会超时
-
结构体重载符号,只能重载一次。所以只能选择写三个比较函数。
-
string不能用scanf来读入,所以可以用中间值转换一下,字符数组。
然后再转回字符串时,需要用到 c_str() (用来将字符数组转为字符串!)
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 100010;
struct Records
{
string id, name;
int grade;
}records[N];
bool cmp1(Records a, Records b)
{
return a.id < b.id;
}
bool cmp2(Records a, Records b)
{
if (a.name != b.name) return a.name < b.name;
return a.id < b.id;
}
bool cmp3(Records a, Records b)
{
if (a.grade != b.grade) return a.grade < b.grade;
return a.id < b.id;
}
int main()
{
int n, c;
scanf("%d%d", &n, &c);
for (int i = 0; i < n; i ++ )
{
char id[10], name[10];
int grade;
scanf("%s %s %d", id, name, &grade);
records[i] = {id, name, grade};
}
if (c == 1) sort(records, records + n, cmp1);
else if (c == 2) sort(records, records + n, cmp2);
else sort(records, records + n, cmp3);
for (int i = 0; i < n; i ++ )
printf("%s %s %d\n", records[i].id.c_str(), records[i].name.c_str(), records[i].grade);
return 0;
}