首页 > 其他 > 详细

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

时间:2014-10-23 23:53:32      阅读:356      评论:0      收藏:0      [点我收藏+]

题目来源: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 数字三角形 最详细的解题报告

原文:http://www.cnblogs.com/pinxiong/p/4045970.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!