题目: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

代码

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<string>
  4. #include<cstring>
  5. using namespace std;
  6. struct Person{
  7. int id;
  8. char name[10];
  9. int score;
  10. }temp[100010];
  11. int C;
  12. bool cmp(Person a, Person b){
  13. switch(C)
  14. {
  15. case 1:return a.id < b.id;
  16. case 2:return a.name!=b.name? strcmp(a.name, b.name) <= 0 : a.id < b.id;
  17. case 3:return a.score!=b.score ? a.score < b.score : a.id < b.id;
  18. }
  19. }
  20. int main(){
  21. int N;
  22. scanf("%d %d",&N,&C);
  23. for(int i = 0; i < N ; i++)
  24. {
  25. scanf("%d %s %d",&temp[i].id,temp[i].name,&temp[i].score);
  26. }
  27. sort(temp, temp + N , cmp);
  28. for(int i = 0; i < N ; i++)
  29. {
  30. printf("%06d %s %d\n",temp[i].id,temp[i].name,temp[i].score);
  31. }
  32. }