import java.util.*; class Student { int chinese, math, english, sum; public Student(int chinese, int math, int english) { this.chinese = chinese; this.math = math; this.english = english; this.sum = chinese + math + english; } } public class Main { // 使用Java的PriorityQueue实现成绩排序 static PriorityQueue<Student> s = new PriorityQueue<>(new Comparator<Student>() { @Override public int compare(Student a, Student b) { // TODO: 实现比较逻辑,按照总分、语文、数学、英语的优先级排序 if(a.sum!=b.sum) { return b.sum-a.sum; }else if(a.chinese!=b.chinese) { return b.chinese-a.chinese; }else if(a.math!=b.math) { return b.math-a.math; }else { return b.english-a.english; } } }); public static void insertValue(int chinese, int math, int english) { // TODO: 实现插入操作 Student student=new Student(chinese, math, english); s.add(student); } public static void deleteValue() { // TODO: 实现删除操作 if(!s.isEmpty()) { s.poll(); } } public static Student getTop() { // TODO: 返回成绩最好的学生 if(!s.isEmpty()) { return s.peek(); } return null; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int q = scanner.nextInt(); while (q-- > 0) { int op = scanner.nextInt(); if (op == 1) { int chinese = scanner.nextInt(); int math = scanner.nextInt(); int english = scanner.nextInt(); insertValue(chinese, math, english); } if (op == 2) { Student top = getTop(); System.out.println(top.chinese + " " + top.math + " " + top.english); } if (op == 3) { deleteValue(); } } scanner.close(); } }
首先把排序的规则写完,然后再去考虑其他的。