Description

7
3 8
8 1 0
2 7 4 4
4 5 2 6 5 (Figure 1)

Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.

Input

Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.

Output

Your program is to write to standard output. The highest sum is written as an integer.

Sample Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Sample Output

30

这道题属于入门级DP,从下往上计算比较方便,递推公式F[i][j]=max{F[i+1][j],F[i+1][j+1]}+num[i][j],初始把最后一行数据赋给F数组,或者直接在num数组上计算。。。

Talk is cheap>>

package poj.dp;

import java.util.Scanner;

/**
* Created with IntelliJ IDEA.
* User: Blank
* Date: 2015/4/2
* Time: 20:42
*/
public class TheTriangle {
public static void main(String[] sure) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] num = new int[n][n];
int[][] cal = new int[n][n];
int i = 0;
while (i < n) {
int j = 0;
while (j <= i) {
num[i][j] = sc.nextInt();
j++;
}
i++;
}
for (int j = 0; j < n; j++) {
cal[n-1][j]=num[n-1][j];
}
for (int j = n - 2; j >= 0; j--) {
for (int k = n - 2; k >= 0; k--) {
cal[j][k]=Math.max(cal[j+1][k],cal[j+1][k+1])+num[j][k];
}
}
System.out.println(cal[0][0]);
}
}

POJ1163——The Triangle的更多相关文章

  1. POJ1163 The Triangle 【DP】

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36918   Accepted: 22117 De ...

  2. (数字三角形)POJ1163 The Triangle

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59698   Accepted: 35792 De ...

  3. POJ1163 The Triangle

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44997   Accepted: 27174 Description 73 ...

  4. Poj1163 The Triangle(动态规划求最大权值的路径)

    一.Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a pro ...

  5. poj-3176 Cow Bowling &&poj-1163 The Triangle && hihocoder #1037 : 数字三角形 (基础dp)

    经典的数塔模型. 动态转移方程:  dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+p[i][j]; #include <iostream> #include ...

  6. POJ1163 The Triangle: 倒三角形问题

    经典的DP问题,DP思想也很直接: 直接贴代码: #include<iostream> #include<cstdio> #include<algorithm> # ...

  7. [LeetCode] Triangle 三角形

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  8. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  9. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

随机推荐

  1. Android应用打包安装过程具体解释

    Android应用打包安装过程(Run as Android Application ): 1,过程:Android Project --> Compilation and Packaging ...

  2. python 学习笔记 基础

    python对象三要素: identity(值):对应于内存的地址,不可修改type(类型):不可修改value(值): mutable :可以修改 immutable:不可以修改 引用计数当引用计数 ...

  3. QT unit test code coverage

    准备环境: qt-creator5.2.1 , gcov(gcc 默认安装),lcov(gcov 的图形化显示界面),qt_testlib 各环境介绍: 1.gcov   gcov 是一个可用于C/C ...

  4. 修改DeDe标签Pagelist分页样式

    我们在用dede仿站的时候,调用文章列表页的分页时,我们会用到: {dede:pagelist listitem=”info,index,end,pre,next,pageno” listsize=” ...

  5. jquer “$” 扩展(笔记)

    /** * Created by shanruo on 16-03-10. */ (function ( $ ){ $.extend ({ /* * 根据参数名获取参数 @name String 参数 ...

  6. Linq101-Conversion Operators

    using System; using System.Linq; namespace Linq101 { class Conversion { /// <summary> /// This ...

  7. (转)MySQL数据库命名规范及约定

    一.[操作规范]1. 如无备注,则表中的第一个id字段一定是主键且为自动增长:2. 如无备注,则数值类型的字段请使用UNSIGNED属性:3. 如无备注,排序字段order_id在程序中默认使用降序排 ...

  8. linux command cp.

    Examples cp file1.txt newdir Copies the file1.txt in the current directory to the newdir subdirector ...

  9. 关于Core Data的一些整理(二)

    关于Core Data的一些整理(二) 创建NSManagedObject的子类时,有一点是在这中间要强调的一点是,要不要勾选 Use scalar properties for primitive ...

  10. 【转】C++之内部类(嵌套类)与外部类及友元

    [转]http://baike.baidu.com/link?url=Md223wQoT5s-3cZ5xRnj1pGmvm310DKAuh-HDrcEdc2l24rwobHrdEc_Mi4Z3BGP0 ...