Java implementation of the Blue Bridge Cup algorithms improve results Sort 2

Questions algorithms improve results Sort 2

Resource constraints
Time limit: 1.0s memory limit: 256.0MB
problem description
  given n student achievement, these students will be sorted by score, collation: High score first; the same score, the higher math scores in the former; Mathematics and the same score, a high front English; English mathematics scores are the same, the former small school number
input format for
  the first line of a positive integer n, the number of students
  next n lines each of 0 to 100 3 integer i-th row represents the number of students in the school i math, English, language scores
output format
  output n rows, each row represents a student's math, English scores, language scores, student number
  in the order sorted output
sample input
2
. 1 2. 3
2. 3. 4
sample output
2 2. 4. 3
. 1 2. 3. 1
data size and conventions
  n≤100

package com.company;

import java.util.Arrays;
import java.util.Scanner;

public class 成绩排序2 {
    public static class Students implements Comparable {
        int Math;
        int English;
        int Chinese;
        int Num;
        int Sum;

        @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.Sum > s.Sum) {
                return 1;
            } else if (this.Sum == s.Sum && this.Math > s.Math) {
                return 1;
            } else if (this.Sum == s.Sum && this.Math == s.Math && this.English > s.English) {
                return 1;
            } else if (this.Math == s.Math && this.English == s.English && this.Sum == s.Sum && 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;
            stu.Sum = stu.Math + stu.English + stu.Chinese;
            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/104252811