解法一:排序

自定义排序规则。Java最后一个点一直被卡,换C++。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Student {
  4. public:
  5. string id;
  6. string name;
  7. int score;
  8. public:
  9. Student() {}
  10. Student(const string &id, const string &name, int score) : id(id), name(name), score(score) {}
  11. static bool cmp1(Student o1, Student o2) {
  12. return o1.id < o2.id;
  13. }
  14. static bool cmp2(Student o1, Student o2) {
  15. if (o1.name == o2.name) {
  16. return o1.id < o2.id;
  17. } else {
  18. return o1.name < o2.name;
  19. }
  20. }
  21. static bool cmp3(Student o1, Student o2) {
  22. if (o1.score == o2.score) {
  23. return o1.id < o2.id;
  24. } else {
  25. return o1.score < o2.score;
  26. }
  27. }
  28. };
  29. Student students[100005];
  30. int main() {
  31. ios::sync_with_stdio(false);
  32. int N, C;
  33. cin >> N >> C;
  34. string id;
  35. string name;
  36. int score;
  37. for (int i = 0; i < N; ++i) {
  38. cin >> id >> name >> score;
  39. students[i] = Student(id, name, score);
  40. }
  41. switch (C) {
  42. case 1:
  43. sort(students, students + N, Student::cmp1);
  44. break;
  45. case 2:
  46. sort(students, students + N, Student::cmp2);
  47. break;
  48. case 3:
  49. sort(students, students + N, Student::cmp3);
  50. }
  51. for (int i = 0; i < N; ++i) {
  52. cout << students[i].id << " " << students[i].name << " " << students[i].score << '\n';
  53. }
  54. return 0;
  55. }