题解 | #成绩排序#
变态题,有名字重复的场景发生,例子没有
这里把名字和加入顺序作为map的key,这样就必然不会有map加入重复key导致数据丢失
这里把名字和加入顺序作为map的key,这样就必然不会有map加入重复key导致数据丢失
垃圾题目!!!如果不给看错误原因,鬼知道
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); int factor = Integer.parseInt(sc.nextLine()); Map<String, String> scoreMap = new HashMap<>(); for (int i = 0; i < n; i++) { String[] strs = sc.nextLine().trim().split(" "); // 华为的变态题,有名字重复的场景发生,例子没有 // 这里把名字和加入顺序作为map的key,必然不会有重复数据的抵消发生 // 垃圾题目!!!如果不给看错误原因,鬼知道! scoreMap.put(strs[0] + " " + i, strs[1]); } List<Map.Entry<String, String>> listEntry = new ArrayList<>(scoreMap.entrySet()); listEntry.sort(new Comparator<Map.Entry<String, String>>() { @Override public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) { int score1 = Integer.parseInt(o1.getValue()); int score2 = Integer.parseInt(o2.getValue()); if (score1 != score2) { if (factor == 1) { return score1 - score2; } else { return score2 - score1; } } else { int start1 = Integer.parseInt(o1.getKey().split(" ")[1]); int start2 = Integer.parseInt(o2.getKey().split(" ")[1]); return start1 - start2; } } }); for (Map.Entry<String, String> entry : listEntry) { System.out.println(entry.getKey().split(" ")[0] + " " + entry.getValue()); } } }