解题思路:
再来一波正解动态规划
注意事项:
参考代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int m = input.nextInt();
int[][] a = new int[m+1][m+1];
int[][] b= new int[m+1][m+1];
for (int i = 0; i <=m; i++) {
for (int j = i; j >0; j--) {
a[i][j]=input.nextInt();
}
}
for (int i =m-1; i>=0;i--) {
for (int j = i; j >0; j--) {
b[i][j]=a[i+1][j]>a[i+1][j+1]?a[i+1][j]+a[i][j]:a[i+1][j+1]+a[i][j];//两种可能要么捡正对下面的,要么捡斜左下的,选大
a[i][j]=b[i][j];
}
}
System.out.println(b[1][1]);
}
}
0.0分
0 人评分