题解HJ68 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
private static Map<String[], Integer> map = new LinkedHashMap<>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = Integer.parseInt(in.nextLine());
int asc = Integer.parseInt(in.nextLine());
for (int i = 0; i < count; i++) {
String[] strs = in.nextLine().split(" ");
map.put(strs, Integer.parseInt(strs[1]));
}
map.entrySet().stream().sorted(((o1, o2) -> {
if (asc == 1) return o1.getValue() - o2.getValue();
else return o2.getValue() - o1.getValue();
})).forEach(x -> System.out.println(x.getKey()[0] + " " + x.getValue()));
}
}
stream自定义排序的简单应用 名字可能会重复所以在输入时选择了数组(虽说这一点我也没看懂)
查看14道真题和解析
携程工作强度 160人发布