* 简单的动态规划问题
* 求路径的最大和,每次只能左下和右下
* 对应到矩阵中,这相当于只能向下或右下
package com.work06;
//The Triangle
import java.util.Scanner;
/**
* 简单的动态规划问题
* 求路径的最大和,每次只能左下和右下
* 对应到矩阵中,这相当于只能向下或右下
*/
public class Demo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()){
int n = sc.nextInt();//输入数据的行数
int nums[][] = new int[n][n];
//输入三角形
for (int i = 0; i < n; i++) {
for (int j = 0; j < i + 1; j++) {
nums[i][j] = sc.nextInt();
}
}
//从倒数第二行开始
for(int t=n-2;t>=0;t--)
{
//遍历当前行行的所有元素(t+1个)
for(int j=0;j<=t;j++)
{
nums[t][j]+=Math.max(nums[t+1][j+1],nums[t+1][j]);
}
}
System.out.println(nums[0][0]);
}
}
}