ブルーブリッジカップアルゴリズムのJava実装は、ソート結果を改善2

質問アルゴリズムは、分類結果を改善2

資源制約の
制限時間:1.0秒メモリ制限:256.0メガバイトの
問題の説明
  与えられたn個の学生の学力、これらの学生はスコア、照合によってソートされます:最初のハイスコア、同じスコア、かつてより高い数学の得点。数学と同じスコア、高い正面英語、英語数学スコアは同じであり、前者の小学校番号
の入力形式
  の正の整数の最初の行のN、学生の数
  次のn行0の各100 3整数i行目は、学校の私の数学の学生の数を表し、英語は、言語スコア
出力形式の
  出力のn行は、各行は学生の数学、英語のスコア、言語スコア、学生の数を表す
  ためには、出力ソートで
サンプルを入力
2
1 2 3
2 3 4
の出力例
2 2 4 3
1 2 3 1
のデータサイズと表記
  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());

    }
}

リリース1101元の記事 ウォンの賞賛6630 ビュー19万+

おすすめ

転載: blog.csdn.net/a1439775520/article/details/104252811