1. 数字三角形
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[][] dp = new int[n + 1][n + 1];
int max = 0;
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j <= i; j++) {
dp[i][j] = scan.nextInt();
dp[i][j] = Math.max(dp[i][j] + dp[i - 1][j], dp[i][j] + dp[i - 1][j - 1]);
max = Math.max(max, dp[i][j]);
}
}
System.out.println(max);
}
}
注意:
- 定义一个
int[][] dp = new int[n + 1][n + 1];
大小为n+1,n+1的二维数组 for (int i = 1; i < dp.length; i++) {
for (int j = 1; j <= i; j++) {