解法一:排序

多标准的排序问题,有个小坑就是分数相同的排名也要相同,分数更低的学生排名就要跳跃了。感觉PAT题目经常隐瞒一些有歧义的地方和数据范围不明说😅。

  1. import java.io.*;
  2. import java.util.*;
  3. public class Main {
  4. // private static float EXP = 1e-5f;
  5. public static void main(String[] args) throws IOException {
  6. StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
  7. PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
  8. in.nextToken();
  9. int N = (int) in.nval;
  10. in.nextToken();
  11. int Q = (int) in.nval;
  12. Map<Integer, Student> students = new HashMap<>(N);
  13. int id;
  14. int C, M, E;
  15. for (int i = 0; i < N; ++i) {
  16. in.nextToken();
  17. id = (int) in.nval;
  18. in.nextToken();
  19. C = (int) in.nval;
  20. in.nextToken();
  21. M = (int) in.nval;
  22. in.nextToken();
  23. E = (int) in.nval;
  24. students.put(id, new Student(C, M, E));
  25. }
  26. Comparator<Student> CComparator = new Comparator<Student>() {
  27. @Override
  28. public int compare(Student o1, Student o2) {
  29. return o2.C - o1.C;
  30. }
  31. };
  32. Comparator<Student> MComparator = new Comparator<Student>() {
  33. @Override
  34. public int compare(Student o1, Student o2) {
  35. return o2.M - o1.M;
  36. }
  37. };
  38. Comparator<Student> EComparator = new Comparator<Student>() {
  39. @Override
  40. public int compare(Student o1, Student o2) {
  41. return o2.E - o1.E;
  42. }
  43. };
  44. Comparator<Student> AComparator = new Comparator<Student>() {
  45. @Override
  46. public int compare(Student o1, Student o2) {
  47. return Float.compare(o2.A, o1.A);
  48. }
  49. };
  50. rank(students.values(), AComparator, 'A');
  51. rank(students.values(), CComparator, 'C');
  52. rank(students.values(), MComparator, 'M');
  53. rank(students.values(), EComparator, 'E');
  54. for (int i = 0; i < Q; ++i) {
  55. in.nextToken();
  56. id = (int) in.nval;
  57. if (students.containsKey(id)) {
  58. out.println(students.get(id));
  59. } else {
  60. out.println("N/A");
  61. }
  62. }
  63. out.flush();
  64. }
  65. private static void rank(Collection<Student> students, Comparator<Student> comparator, char type) {
  66. List<Student> list = new ArrayList<>(students);
  67. list.sort(comparator);
  68. list.get(0).updateRank(1, type);
  69. int rank = 1;
  70. for (int i = 1; i < list.size(); ++i) {
  71. switch (type) {
  72. case 'A':
  73. if (list.get(i).A < list.get(i - 1).A) {
  74. rank = i + 1;
  75. }
  76. break;
  77. case 'C':
  78. if (list.get(i).C < list.get(i - 1).C) {
  79. rank = i + 1;
  80. }
  81. break;
  82. case 'M':
  83. if (list.get(i).M < list.get(i - 1).M) {
  84. rank = i + 1;
  85. }
  86. break;
  87. case 'E':
  88. if (list.get(i).E < list.get(i - 1).E) {
  89. rank = i + 1;
  90. }
  91. break;
  92. }
  93. list.get(i).updateRank(rank, type);
  94. }
  95. }
  96. }
  97. class Student {
  98. int C;
  99. int M;
  100. int E;
  101. float A;
  102. int bestRank;
  103. char type;
  104. Student(int C, int M, int E) {
  105. this.C = C;
  106. this.M = M;
  107. this.E = E;
  108. A = (float) (C + M + E) / 3.0f;
  109. bestRank = 0x3f3f3f3f;
  110. }
  111. void updateRank(int rank, char type) {
  112. if (rank < bestRank) {
  113. bestRank = rank;
  114. this.type = type;
  115. }
  116. }
  117. @Override
  118. public String toString() {
  119. return bestRank + " " + type;
  120. }
  121. }