问题描述
给出n个学生的成绩,将这些学生按成绩排序,
排序规则,优先考虑数学成绩,高的在前;数学相同,英语高的在前;数学英语都相同,语文高的在前;三门都相同,学号小的在前
输入格式
第一行一个正整数n,表示学生人数
接下来n行每行3个0~100的整数,第i行表示学号为i的学生的数学、英语、语文成绩
输出格式
输出n行,每行表示一个学生的数学成绩、英语成绩、语文成绩、学号
按排序后的顺序输出
样例输入
2
1 2 3
2 3 4
样例输出
2 3 4 2
1 2 3 1
数据规模和约定
n≤100
解题思路:
纯排序,直接封装成类,调用Comparable接口,重写compareTo()方法
java代码:
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
List<Temp290> list = new ArrayList<>();
for(int i = 1; i <= n;i++) {
String[] split = br.readLine().split(" ");
int math = Integer.parseInt(split[0]);
int english = Integer.parseInt(split[1]);
int chinese = Integer.parseInt(split[2]);
list.add(new Temp290(math, english, chinese, i));
}
Collections.sort(list);
for(Temp290 data : list) {
System.out.println(data);
}
}
}
class Temp290 implements Comparable<Temp290>{
int math;
int english;
int chinese;
int num;
public Temp290(int math, int english, int chinese, int num) {
this.math = math;
this.english = english;
this.chinese = chinese;
this.num = num;
}
@Override
public String toString() {
return math + " " + english + " " + chinese + " " + num;
}
@Override
public int compareTo(Temp290 o) {
if(this.math != o.math) {
return o.math - this.math;
}else if(this.english != o.english) {
return o.english - this.english;
}else if(this.chinese != o.chinese) {
return o.chinese - this.chinese;
}else {
return this.num - o.num;
}
}
}