POJ1163 The Triangle 【DP】
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 36918 | Accepted: 22117 |
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
all integers, are between 0 and 99.
Output
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
题意:入门级数塔。
题解:用数组存储起点到任一点的最大值为后面的计算提供便利。
#include <stdio.h> int dp[102][102]; int max(int a, int b){ return a > b ? a : b; } int main()
{
int n, i, j, ans;
scanf("%d", &n);
for(i = 1; i <= n; ++i){
for(j = 1; j <= i; ++j){
scanf("%d", &dp[i][j]);
dp[i][j] += max(dp[i-1][j], dp[i-1][j-1]);
}
} ans = 0;
for(i = 1; i <= n; ++i)
if(dp[n][i] > ans) ans = dp[n][i];
printf("%d\n", ans);
return 0;
}
POJ1163 The Triangle 【DP】的更多相关文章
- Kattis - honey【DP】
Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- HDOJ 1501 Zipper 【DP】【DFS+剪枝】
HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDOJ 1257 最少拦截系统 【DP】
HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDOJ 1159 Common Subsequence【DP】
HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】
HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】
POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...
- HackerRank - common-child【DP】
HackerRank - common-child[DP] 题意 给出两串长度相等的字符串,找出他们的最长公共子序列e 思路 字符串版的LCS AC代码 #include <iostream&g ...
- LeetCode:零钱兑换【322】【DP】
LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...
随机推荐
- Swift - 使用arc4random()、arc4random_uniform()取得随机数
arc4random()这个全局函数会生成9位数的随机整数 1,下面是使用arc4random函数求一个1~100的随机数(包括1和100) 1 var temp:Int = Int(arc4ra ...
- dsplib使用备忘
1. 到TI官网下载与自己的芯片对应的dsplib库 2. 在工程属性里添加dsplib的头文件路径,lib库路径 3. 在源文件中包含dsplib头文件,如果是在.cpp文件里调用,包含头文件时要用 ...
- ThinkPhp学习09
原文:ThinkPhp学习09 三.区间查询 $data['id']=array(array('gt',4),array('lt',10));//默认关系是 and 的关系 //SELECT * FR ...
- UVA 10003 Cutting Sticks
题意:在给出的n个结点处切断木棍,并且在切断木棍时木棍有多长就花费多长的代价,将所有结点切断,并且使代价最小. 思路:设DP[i][j]为,从i,j点切开的木材,完成切割需要的cost,显然对于所有D ...
- 《Head First 设计模式》学习笔记——模板方法模式
模板方法模式是类的行为模式.准备一个抽象类,将部分逻辑以详细方法以及详细构造函数的形式实现.然后声明一些抽象方法来迫使子类实现剩余的逻辑.不同的子类能够以不同的方式实现这些抽象方法,从而对剩余的逻辑有 ...
- cocos2d-x-lua基础系列教程四(lua多继承)
lua - 多继承 1,模拟伪继承 新建extend_test.lua 新建extend_test.lua setmetable(a,b) b对象是a 对象的父类 a继承于b Account = { ...
- Delphi的没落有三个原因(比较贴切)
Delphi的没落有三个原因:1,Object pascal并没有像C/C++那样成为业内标准. 如果你使用C或C++写程序,可以在任何地方进行编译,例如Windows.Linux.手机,甚至是单片机 ...
- android画笔错位问题的解决
下面的画画板的代码: public class MainActivity extends Activity { private ImageView iv; private Bitmap baseBit ...
- oracle 之 内存—鞭辟近里(三)
oracle 之 内存—鞭辟近里(三) 今天是2013-07-08,今天晚上突然接到一个电话,我的外甥问我的qq是多少,我感觉很吃惊,他长大了.在他现在这个年龄就开始接触网络,我难免有少许担心,希望他 ...
- HDU4685 Prince and Princess 完美搭配+良好的沟通
意甲冠军:今天,有n王子,m公主.现在给他们配对,与王子会嫁给一个男人,他喜欢.公主无法做出选择. 这标题去咬硬,还有一类似的题目poj1904.那个题目也是给王子与公主配对,但那个是王子公主各n个, ...