解题思路:
注意事项:
注意重写compareTo()时,是升序还是降序
参考代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class 奖学金 {
public static class S implements Comparable<S> {
int id;
int a;
int b;
int c;
public S(int id, int a, int b, int c) {
this.id = id;
this.a = a;
this.b = b;
this.c = c;
}
public int getSum() {
return a + b + c;
}
@Override
public int compareTo(S o) {
int k = 0;
k = Integer.compare(o.getSum(), getSum());
if (k != 0) {
return k;
}
k = Integer.compare(o.a, a);
if (k != 0) {
return k;
}
return Integer.compare(id, o.id);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<S> list = new ArrayList<>(256);
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
list.add(new S(i + 1, a, b, c));
}
list.sort(null);
for (int i = 0; i < 5; i++) {
S s = list.get(i);
System.out.println(s.id + " " + s.getSum());
}
}
}
0.0分
0 人评分
【简单计算】 (C语言代码)浏览:642 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:913 |
GC的苦恼 (C语言代码)浏览:672 |
C语言程序设计教程(第三版)课后习题10.5 (C语言代码)浏览:985 |
C语言训练-列出最简真分数序列* (C语言代码)浏览:658 |
简单的a+b (C语言代码)浏览:672 |
2004年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:617 |
C语言程序设计教程(第三版)课后习题11.1 (C++代码)浏览:709 |
A+B for Input-Output Practice (C语言代码)浏览:489 |
Manchester-汽水瓶问题浏览:4235 |