Questions sort algorithm to improve performance
Resource constraints
Time limit: 1.0s memory limit: 256.0MB
problem description
given n student achievement, these students will be sorted by score,
collation, priority math, high in the former; the same mathematics, English high front ; English mathematics are the same, the language of the former high; three are the same, the front small student number
input format for
the first line of a positive integer n, the number of students
next n lines each line an integer of 0 to 3 100, i-th row represents the number of students in school i math, English, language scores
output format
output n rows, each row represents a student's math, English scores, language scores, student number
output in sorted order
sample input
2
2. 3. 1
2. 3. 4
sample output
2 2. 4. 3
. 1 2. 3. 1
data size and conventions
n≤100
PS: multi-use several methods, a change of thinking
import java.util.Arrays;
import java.util.Scanner;
public class 成绩排序 {
public static class Students implements Comparable{
int Math;
int English;
int Chinese;
int Num;
@Override
public String toString() {
return this.Math+" "+this.English+" "+this.Chinese+" "+this.Num;
}
@Override
public int compareTo(Object o) {
Students s= (Students) o;
if (this.Math>s.Math){
return 1;
}else if(this.Math==s.Math && this.English>s.English){
return 1;
}
else if (this.Math==s.Math && this.English==s.English && this.Chinese>s.Chinese){
return 1;
}
else if (this.Math==s.Math && this.English==s.English && this.Chinese==s.Chinese && this.Num<s.Num){
return 1;
}
return -1;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Students [] students = new Students[n];
for (int i=0;i<n;i++){
Students stu = new Students();
stu.Math=sc.nextInt();
stu.English=sc.nextInt();
stu.Chinese=sc.nextInt();
stu.Num=i+1;
students[i]=stu;
}
Arrays.sort(students);
for (int i=n-1;i>=0;i--)
System.out.println(students[i].toString());
}
}