木辛成红


私信TA

用户名:dotcpp0760855

访问量:44

签 名:

等  级
排  名 12946
经  验 899
参赛次数 0
文章发表 2
年  龄 0
在职情况 学生
学  校 汕头大学
专  业

  自我简介:

TA的其他文章

重写compare方法
浏览:16

解题思路:

注意事项:

参考代码:

import java.util.Arrays;

import java.util.Comparator;

import java.util.Scanner;


class Stu{

int id;

int chinese;

int math;

int english;

int sum;

public Stu(int id,int chinese,int math,int english) {

this.id = id;

this.chinese = chinese;

this.math = math;

this.english = english;

this.sum = chinese + math + english;

}

}

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();

Stu[] students = new Stu[n];

for (int i = 0; i < n; i++) {

students[i] = new Stu(i + 1, scanner.nextInt(), scanner.nextInt(), scanner.nextInt());

}

Arrays.sort(students,new Comparator<Stu>() {

@Override

public int compare(Stu stu1,Stu stu2) {

if (stu1.sum == stu2.sum ) {

if (stu1.chinese == stu2.chinese) {

return stu1.id - stu2.id;

}

else {

return stu2.chinese - stu1.chinese;

}

}

return stu2.sum - stu1.sum;

}

});

for (int i = 0; i < 5; i++) {

System.out.println(students[i].id + " " + students[i].sum);

}

}

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区