题目链接:
https://pintia.cn/problem-sets/994805342720868352/problems/994805468327690240
题目分析:
多混合排序问题。对不同键值进行排序。问题是写比较函数。注意:若将姓名定义成string类型超时。 不知道大家有没有出现这样的情况。望指正。string类型无法用printf以及scanf 输入或者输出。
参考代码:
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
struct record{
int score,id;
char name[10];
}stu[100010];
int n,col;
bool cmp(record a, record b){
if(col == 1) return a.id<b.id;
else if(col == 2) return strcmp(a.name, b.name) != 0 ? strcmp(a.name, b.name) < 0 : a.id < b.id;
else return a.score != b.score ? a.score < b.score : a.id<b.id;
}
int main(){
scanf("%d %d", &n, &col);
for(int i=0; i < n; i++)
scanf("%d %s %d",&stu[i].id, stu[i].name, &stu[i].score);
sort(stu, stu + n, cmp);
for(int i = 0; i < n; i++){
printf("%06d %s %d\n",stu[i].id, stu[i].name, stu[i].score);
}
return 0;
}