普及练习场 排序Ex 奖学金

来源:互联网 发布:蚁群算法解决指派问题 编辑:程序博客网 时间:2024/05/29 13:22

题目链接

题意理解

没什么看不懂的地方吧。。。

代码

import java.util.Arrays;import java.util.Collections;import java.util.Scanner;class Student implements Comparable{    int chinese;    int sum;    int index;    public Student(int chinese, int math, int english, int index) {        this.chinese = chinese;        this.sum = chinese + math + english;        this.index = index;    }    @Override    public int compareTo(Object o) {        Student student = (Student)o;        if(this.sum != student.sum) {            return this.sum - student.sum;        } else {            if(this.chinese != student.chinese) {                return this.chinese - student.chinese;            } else {                return student.index - this.index;            }        }    }}public class Main {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        int n = scanner.nextInt();        Student[] students = new Student[n];        int chinese;        int math;        int english;        for(int i = 0; i < n; i++) {            chinese = scanner.nextInt();            math = scanner.nextInt();            english = scanner.nextInt();            students[i] = new Student(chinese, math, english, i + 1);        }        scanner.close();        Arrays.sort(students, Collections.reverseOrder());        for(int i = 0; i < 5; i++) {            System.out.println(students[i].index + " " + students[i].sum);        }    }}

欢迎加入“不会算法一群菜鸟”,群号是⑥⑥①⑨②2025,这是我设置的一道很低的门槛用来阻止广告的。入群的验证暗号是:我爱编译原理