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

题解:求取数字三角形从顶到底和最大的一条路,每次只能往左下或右下走,那我们的思路就是从底到顶动态规划即可,每次只需要取最大的,那到顶一定是最大的

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring> using namespace std; int main()
{
int n;
scanf("%d",&n);
int dp[105][105];
for(int t=0;t<n;t++)
{
for(int j=0;j<=t;j++)
{ scanf("%d",&dp[t][j]);
}
}
for(int t=n-2;t>=0;t--)
{
for(int j=0;j<=t;j++)
{
dp[t][j]+=max(dp[t+1][j+1],dp[t+1][j]);
}
}
printf("%d\n",dp[0][0]); return 0;
}

The Triangle (简单动态规划)的更多相关文章

  1. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  2. 简单动态规划——三逆数的O(N^2)解法!

    [算法]简单动态规划——三逆数的O(N^2)解法! 问题描述: 三逆数定义:给一个数的序列A[0,1,....N-1]),当i<j<k且A[i]>A[j]>A[k]时,称作ai ...

  3. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  4. 【算法】简单动态规划——三逆数的O(N^2)解法!

    问题描述: 三逆数定义:给一个数的序列A[0,1,....N-1]),当i<j<k且A[i]>A[j]>A[k]时,称作ai,aj,ak为一个三逆数. 现在给定一个长度为N的数 ...

  5. 51nod 1270 数组的最大代价 思路:简单动态规划

    这题是看起来很复杂,但是换个思路就简单了的题目. 首先每个点要么取b[i],要么取1,因为取中间值毫无意义,不能增加最大代价S. 用一个二维数组做动态规划就很简单了. dp[i][0]表示第i个点取1 ...

  6. HDU 1176 免费馅饼 简单动态规划

    世道很简单的动态规划,但是却错了,让我很无语,改来改去还是不对,第二天有写就对了,之后我就耐着性子慢慢比较之前的错误代码,发现 第一次错:纯粹用了a[i][j]+=max3(a[i+1][j-1], ...

  7. POJ - 1163 The Triangle 【动态规划】

    一.题目 The Triangle 二.分析 动态规划入门题. 状态转移方程$$DP[i][j] = A[i][j] + max(DP[i-1][j], DP[i][j])$$ 三.AC代码 1 #i ...

  8. HDUOJ----2571(命运)(简单动态规划)

    命运 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  9. Triangle(动态规划)

    题目描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...

随机推荐

  1. 团队项目-记账App

    一.团队成员介绍 队长: 向瑜 博客园地址: https://www.cnblogs.com/xiangyu721/  沟通能力较强,善于总结,能够正确分配团队任务.其次,有耐心学习新事物,发现新问题 ...

  2. Spring Boot Logback 默认配置

    Spring Boot Logback 默认配置 标签(空格分隔): Spring Boot Intro(介绍) Spring Boot 1.5.9 默认使用的日志框架是 Logback. 生效的默认 ...

  3. Java自学-图形界面 容器

    Swing 的容器 JFrame和JDialog java的图形界面中,容器是用来存放 按钮,输入框等组件的. 窗体型容器有两个,一个是JFrame,一个是JDialog 步骤 1 : JFrame ...

  4. 【API进阶之路】帮公司省下20万调研费!如何巧用情感分析API实现用户偏好调研

    摘要:自从学习API后,仿佛解锁了新技能,可别小看了一个小小的API接口,用好了都是能力无穷.这不,用情感分析API来做用户偏好调研,没想到这么一个小创意给公司省了20万调研费用. 上次借着高考热点整 ...

  5. MySQL数据库的约束

    一 默认值约束 约束语句 default  ‘默认值’ 在建立表的时候在想要加默认约束的字段名,数据类型后面加default ‘默认值’ 例如 : create table emp( uid  int ...

  6. 后端排序时去掉element表格排序的null状态

    经常会遇到远程排序,需要去掉null状态的排序,当设置sortable='custom'时,设置sort-orders为['ascending', 'descending']是不生效的.然后查到了一种 ...

  7. Oracle用户授权

    一.用户授权 1)普通权限 grant ${autoType1, autoType2, autoType3, ...} to ${userName} identified by ${password} ...

  8. TestLink使用指南

    TestLink安装上之后,局域网内用户可以登陆使用,下面介绍本软件的使用方式. 1.TestLink简介 TestLink是基于Web的开源测试管理工具,用户可以使用这个工具创建测试项目和测试用例, ...

  9. windows 下apache开启FastCGI

    1.首先去(http://www.apachelounge.com/download/)下载一个合适的mod_fcgid  文件.     2.将解压后的文件改为mod_fcgid.dll 并复制到a ...

  10. CSS动画实例:行星和卫星

    设页面中有<div class=" planet "></div>,用来绘制一个行星和卫星图形.这个图形包括三部分:行星.卫星和卫星旋转的轨道.定义. pl ...