hxqy1998 @ 2020-03-04 10:59:07
不知道是数据量太大数组占内存太大,还是因为nextInt方法花销太大,如果是nextInt,这已经不是第一次了,所以有没有哪位大佬帮帮忙,怎么能低耗快速读取键盘输入,感激不尽
import java.util.Scanner;
public class Main{
private static int[][] arr;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
arr = new int[n][];
for (int i = 0; i < n; i++) {
arr[i] = new int[i + 1];
for(int j = 0; j <= i; j++){
arr[i][j] = in.nextInt();
}
}
System.out.println(findMax(n));
}
private static int findMax(int row){
if(row == 1) return arr[0][0];
for(int i = 1; i < row; ++i){
for(int j = 0; j < arr[i].length; ++j){
if(j == 0){
arr[i][j] += arr[i - 1][j];
}else if(j == arr[i].length - 1){
arr[i][j] += arr[i - 1][j - 1];
}else {
arr[i][j] += Math.max(arr[i - 1][j], arr[i - 1][j - 1]);
}
}
}
int max = Integer.MIN_VALUE;
for(int i = 0; i < arr[row - 1].length; ++i){
if(arr[row - 1][i] > max) max = arr[row - 1][i];
}
return max;
}
}
by YUYGFGG @ 2020-03-04 11:01:31
爪哇
by huanyue @ 2020-03-04 11:26:06
只会C++
by Then @ 2020-03-09 15:32:09
CSDN里面有很多关于java 快速io的模板,比如https://blog.csdn.net/hhczy1003/article/details/49854441 随手找的,也许有简单版本的
by Tyler0819 @ 2020-04-10 22:04:53
抱歉,我是c++OIer,我不会JAVA
唉,多会一门语言多好,尤其是C++
by SUPERMAN0109 @ 2020-05-30 08:48:21
我也是C++的