题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805468327690240
思路
这题比较简单,思路与柳基本一致
一个注意点:题目中的描述是non-decrease order,非降序排序,改成了strcmp(a.name, b.name) <=0后通过了
注意,按照名称的不降序排序,因为strcmp比较的是ACSII码,所以A < Z。写cmp函数的时候return strcmp(a.name, b.name) <= 0; return语句返回的是true或者false的值,所以要写 <= 0 这样的形式。比较ACSII码的大小,strcmp(‘a’, ‘z’)返回负值,因为a<z a – z < 0
代码
#include<iostream>#include<algorithm>#include<string>#include<cstring>using namespace std;struct Person{int id;char name[10];int score;}temp[100010];int C;bool cmp(Person a, Person b){switch(C){case 1:return a.id < b.id;case 2:return a.name!=b.name? strcmp(a.name, b.name) <= 0 : a.id < b.id;case 3:return a.score!=b.score ? a.score < b.score : a.id < b.id;}}int main(){int N;scanf("%d %d",&N,&C);for(int i = 0; i < N ; i++){scanf("%d %s %d",&temp[i].id,temp[i].name,&temp[i].score);}sort(temp, temp + N , cmp);for(int i = 0; i < N ; i++){printf("%06d %s %d\n",temp[i].id,temp[i].name,temp[i].score);}}
