java题解(重写排序方法)
import java.util.Arrays;
import java.util.Scanner;
public class 奖学金 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
students arr[] = new students[n + 1];
for (int i = 0; i < n; i++) {
int l = sc.nextInt();
int m = sc.nextInt();
int e = sc.nextInt();
arr[i] = new students(l, m, e, i + 1, l + m + e);
}
Arrays.sort(arr, 0, n);
for (int i = n - 1; i >= n - 5; i--) {
if (i < 0) {
return;
}
System.out.println(arr[i].stno + " " + arr[i].sum);
}
}
}
class students implements Comparable<students> {
int language;
int math;
int english;
int sum;
int stno;
public students(int l, int m, int e, int stn, int sum) {
this.language = l;
this.math = m;
this.english = e;
this.stno = stn;
this.sum = sum;
}
@Override
public int compareTo(students oth) { //重写compareto方法
// TODO Auto-generated method stub
if (this.sum == oth.sum) { //总分一样时,比较语文成绩
if (this.language == oth.language) { //语文成绩一样时比较学号,这里要注意,学号是从大到小排序
return oth.stno - this.stno; //换个位置
} else {
return this.language - oth.language;
}
} else {
return this.sum - oth.sum;
}
}
}