Java implementation of the Blue Bridge Cup improve performance sorting algorithm

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());

    }
}

Released 1101 original articles · won praise 6630 · Views 190,000 +

Guess you like

Origin blog.csdn.net/a1439775520/article/details/104252647