题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.*;
// 使用map,记录成绩和人名
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
int mode = Integer.parseInt(in.nextLine());
HashMap<Integer, String> map = new HashMap<>();
int[] arr = new int[n];
int repeat = 0;//记录有多少重复数字
for (int i = 0; i < n ; i++) {
String s = in.nextLine();
String[] arrS = s.split(" ");
if (!map.containsKey(Integer.parseInt(arrS[1]))) {
map.put(Integer.parseInt(arrS[1]), arrS[0]);
arr[i - repeat] = Integer.parseInt(arrS[1]);
} else {//如果成绩重复,那就把之前的人名和成绩拼起来,中间加上换行符~
map.put(Integer.parseInt(arrS[1]),
map.get(Integer.parseInt(arrS[1])) + " " + arrS[1] + "\n" + arrS[0]);
repeat++;
}
}
int[] arr2 = new int[n - repeat];
for (int i = 0; i < n - repeat ; i++) {
arr2[i] = arr[i];
}
Arrays.sort(arr2);
if (mode == 1) {
for (int i = 0; i < n - repeat ; i++) {
System.out.println(map.get(arr2[i]) + " " + arr2[i]);
}
} else {
for (int i = n - repeat - 1; i >= 0 ; i--) {
System.out.println(map.get(arr2[i]) + " " + arr2[i]);
}
}
}
}
