思路
定义Student类存储编号,各科成绩和总分,实现Comparable接口,自定义排序规则
然后调用sort方法直接排序
java 代码
import java.io.*;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
Student[] ss = new Student[n];
for (int i = 0; i < n; i++) {
int[] line = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
ss[i] = new Student(i + 1, line[0], line[1], line[2], line[0] + line[1] + line[2]);
}
Arrays.sort(ss);
for (int i = 0; i < 5; i++) {
bw.write(ss[i].id + " " + ss[i].scoreSum + "\n");
}
bw.flush();
bw.close();
}
}
class Student implements Comparable<Student> {
int id;
int chinese;
int math;
int english;
int scoreSum;
public Student(int id, int chinese, int math, int english, int scoreSum) {
this.id = id;
this.chinese = chinese;
this.math = math;
this.english = english;
this.scoreSum = scoreSum;
}
@Override
public int compareTo(Student o) {
int x = o.scoreSum - this.scoreSum;
if (x == 0) {
x = o.chinese - this.chinese;
}
if (x == 0) {
x = this.id - o.id;
}
return x;
}
}