NYOJ 题目18 The Triangle
2016-05-19 21:51
253 查看
描述
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.
输入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.
输出Your program is to write to standard output. The highest sum is written as an integer.
样例输入
样例输出
分析:简单的动态规划,注意只能往右,往下就行了。
AC代码:
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.
输入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.
输出Your program is to write to standard output. The highest sum is written as an integer.
样例输入
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
样例输出
30
分析:简单的动态规划,注意只能往右,往下就行了。
AC代码:
#include<stdio.h> #include<iostream> using namespace std; int dp[110][110]; int main() { int n,i,j; int a[110][110]; while(~scanf("%d",&n)) { for(i=0;i<n;i++) for(j=0;j<=i;j++) scanf("%d",&a[i][j]); for(i=1;i<=n;i++) for(j=1;j<=i;j++) { dp[i][j]=max(dp[i-1][j]+a[i-1][j-1],dp[i-1][j-1]+a[i-1][j-1]); } int max; max=dp [1]; for(i=1;i<n;i++) { if(dp [i]>max) max=dp [i]; } printf("%d\n",max); } return 0; }
相关文章推荐
- 操作系统知识(2)--Linux 系统目录结构
- 回文数组
- matlab 函数
- Spark笔记:RDD基本操作(下)
- Directx11学习笔记【十三】 实现一个简单地形
- 今天聊聊大数据
- 常见链表面试题之从尾到头打印链表
- 局部特征检测算子综述
- laravel5.1框架下实现图片上传
- git-canal:学习记录2
- Java RandomAccessFile用法
- java初步了解
- leetcode-24-Swap Nodes in Pairs
- 嵌入式Qt开发环境搭建及移植到开发板----Qt学习笔记
- HDOJ 4911-Inversion【逆序对模板】
- Codeforces Round #353 (Div. 2) -- B. Restoring Painting (规律)
- Unable to run mksdcard SDK tool
- .obj文件格式与.mtl文件格式
- include和require加载文件的区别
- String深度解析