问题描述
给出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
import java.util.Arrays;
import java.util.Scanner;
public class 成绩排序 {
public static class Student implements Comparable<Object>{
int gm = 0;//数学成绩
int ge = 0;//英语成绩
int gy = 0;//语文成绩
int num = 0;//编号
public String toString() {
return this.gm+" "+this.ge+" "+this.gy+" "+this.num;
}
//排序规则
public int compareTo(Object o) {
Student s = (Student)o;
if(this.gm>s.gm)//优先考虑数学成绩,高的在前
{
return 1;
}else if (this.gm==s.gm&&this.ge>s.ge) {//数学相同,英语高的在前
return 1;
}else if (this.gm==s.gm&&this.ge==s.ge&&this.gy>s.gy) {//数学英语都相同,语文高的在前
return 1;
}else if (this.gm==s.gm&&this.ge==s.ge&&this.gy==s.gy&&this.num<s.num) {//三门都相同,学号小的在前
return 1;
}
return -1;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Student[] students = new Student[n];
//读入数据
for(int i = 0;i<n;i++)
{
Student student = new Student();
student.gm = sc.nextInt();
student.ge = sc.nextInt();
student.gy = sc.nextInt();
student.num = i+1;
students[i] = student;
}
//排序,默认是按升序排序
Arrays.sort(students);
//反过来,按照成绩的降序排序
for(int i = n-1;i>=0;i--)
{
System.out.println(students[i].toString());
}
sc.close();
}
}