hihoCoder 1037 数字三角形 最详细的解题报告

题目来源:hihoCoder 1037 数字三角形

解题思路:请好好看看 提示一、提示二、提示三

 

具体算法(java版,可以直接AC)

import java.util.Scanner;

public class Main {

    public static int[][] rewards;
    public static int[][] best;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        rewards = new int[n + 1][n + 1]; //下标均从1开始
        best = new int[n + 1][n + 1]; 
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                rewards[i][j] = scanner.nextInt();
            }
        }
        int ans = Integer.MIN_VALUE;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                best[i][j] = Math.max(best[i - 1][j - 1], best[i - 1][j])
                        + rewards[i][j];
                if (ans < best[i][j]) {
                    ans = best[i][j];
                }
            }
        }
        System.out.println(ans);
    }
}

 

你可能感兴趣的:(hihoCoder 1037 数字三角形 最详细的解题报告)