题解 | 成绩排序
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.Arrays; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); int b = in.nextInt(); in.nextLine(); int[][]stu = new int[a][2]; Map<Integer,String> map = new HashMap<>(); for (int i = 0; i < a; i++) { String s = in.nextLine(); String []ss = s.split(" "); map.put(i,ss[0]); stu[i][0] = i; stu[i][1] = Integer.parseInt(ss[1]); } if (b == 0) { Arrays.sort(stu, (c, d)->d[1] - c[1]); } else { Arrays.sort(stu, (c, d)->c[1] - d[1]); } for (int i = 0; i < a; i++) { for (Map.Entry<Integer,String> e : map.entrySet()) { if (e.getKey() == stu[i][0]) { System.out.println(e.getValue() + " " + stu[i][1]); } } } } } }