POJ 1163 The Triangle
2015-08-01 23:08
337 查看
Description
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
Sample Output
30
简单dp
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
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<cmath> #include<queue> #include<vector> #include<map> #include<set> using namespace std; const int maxn = 105; int n, f[maxn][maxn], a[maxn][maxn]; int main() { while (~scanf("%d", &n)) { memset(f, 0, sizeof(f)); for (int i = 1; i <= n;i++) for (int j = 1; j <= i; j++) scanf("%d", &a[i][j]); for (int i = n; i; i--) for (int j = 1; j <= i; j++) f[i][j] = a[i][j] + max(f[i + 1][j], f[i + 1][j + 1]); printf("%d\n", f[1][1]); } return 0; }
相关文章推荐
- POJ 1837 Balance
- hdu 5336 XYZ and Drops
- iOS多线程——GCD篇
- SP2-0618 SP2-0611 ORA-01919 开执行计划错误(oracle12c)
- MongoDB分布式
- 一种二叉树前序非递归遍历方法
- Android Studio中集成OpenCV——只需4步
- SCCM2012软件中心的“从应用程序目录中查找其他应用程序”打不开的解决方法
- POJ 2385 Apple Catching
- Path Sum II
- poj 1141 Brackets Sequence
- Odoo(OpenERP)开发实践:通过XML-RPC接口访问Odoo数据库
- apache2学习笔记
- 论文笔记《Feedforward semantic segmentation with zoom-out features》
- hdu 5335 Walk Out
- Cocos2dx如何实现每一帧的触发
- listview的一个聊天界面
- uvalive 4986(三分查找)
- MongoDB导出
- [设计模式]<<设计模式之禅>>关于单例模式