麻烦bobo老师帮我看下蓝桥杯的一个题目


相关代码:
在网上也看了很多解法,感觉和自己的思路是一样的,为什么我这个过不去。。。。实在不解了,麻烦bobo老师帮我看下,谢谢bobo老师
源自:非比较排序
1-1 什么是计数排序
17
收起
正在回答
1回答
你已经说了网上看到的解法和自己的思路一样了,所以问题可能就不在思路上了,而在具体实现上了。你贴给我一个错误用例,我是不知道你的问题是什么的。
请:
1)贴出具体 OJ 链接
2)贴出你的错误代码
方便我调试。
==========
我不确定你是不是真的理解了这个问题的思路。但是,sort 中的 compare,只需要按照每个学生的三个属性之和进行排序就可以了,即排序这样写即可:
Arrays.sort(student, new Comparator<int[]>() {
public int compare(int[] student1, int[] student2) {
return (student1[0] + student1[1] + student1[2]) - (student2[0] + student2[1] + student2[2]);
}});另外,由于结果比较大,你的代码中的 sum 和 time 需要声明为 long。
我在你的代码的基础上修改可以 ac 的程序:
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
//1:无需package
//2: 类名必须Main, 不可修改import java.util.function.IntPredicate;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[][] student = new int[n][3];
for(int i = 0; i < n; i ++) {
for(int j = 0; j < 3; j ++) {
student[i][j] = scan.nextInt();
}
}
// 排序方式有变动
Arrays.sort(student, new Comparator<int[]>() {
public int compare(int[] student1, int[] student2) {
return (student1[0] + student1[1] + student1[2]) - (student2[0] + student2[1] + student2[2]);
}});
// sum 和 time 需要 long 类型
long sum = 0;
long[] time = new long[n];
for(int i = 0; i < n; i ++) {
if(i == 0) {
time[0] = student[i][0] + student[i][1];
}else {
time[i] = time[i - 1] + student[i - 1][2] + student[i][0] + student[i][1];
}
}
for(int i = 0; i < n; i ++) {
sum += time[i];
}
//在此输入您的代码...
scan.close();
System.out.println(sum);
}
}继续加油!:)
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星