NYOJ题目18-The Triangle(经典dp)
来源:互联网 发布:音乐剪辑合成软件 编辑:程序博客网 时间:2024/05/20 13:38
The Triangle
时间限制:1000 ms | 内存限制:65535 KB
难度:4
- 描述
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.
- 样例输入
573 88 1 0 2 7 4 44 5 2 6 5
- 样例输出
30
一层一层累加:dp[i][j] =a[i][j] + (dp[i-1][j-1]>dp[i-1][j] ?dp[i-1][j-1]:dp[i-1][j]);
#include <cstdio>#include <cstring>#include <iostream>using namespace std;int dp[105][105];int a[105][105];int main(){int n;while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++)for(int j=0;j<i+1;j++)cin>>a[i][j];memset(dp,0,sizeof(dp));int max;for(int i=0;i<n;i++){for(int j=0;j<i+1;j++){dp[i][j] =a[i][j] + (dp[i-1][j-1]>dp[i-1][j] ?dp[i-1][j-1]:dp[i-1][j]);//cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;;}}max = dp[n-1][0];for(int j=0;j<n;j++){if(dp[n-1][j]>max)max=dp[n-1][j];}cout<<max<<endl;}return 0;}
阅读全文
0 0
- NYOJ题目18-The Triangle(经典dp)
- nyoj 18 The Triangle 【经典dp】
- NYOJ - The Triangle(经典dp)
- NYOJ 题目18 The Triangle
- NYOJ 题目18 The Triangle
- NYOJ 18 The Triangle(简单dp)
- nyoj The Triangle 18 (简单DP)
- NYOJ 题目18 The Triangle(数塔)
- NYOJ 18 The Triangle(dp + 记忆化)
- nyoj--18--The Triangle(dp水题)
- NYOJ 18-The Triangle(典型DP)
- NYOJ 18. The triangle(基础DP)
- The Triangle 经典DP
- The Triangle 经典DP
- NYOJ 18 The Triangle
- NYOJ 18-The Triangle
- The Triangle(nyoj 18)
- nyoj-18-The Triangle
- mac 远程访问window
- ThinkPHP的学习
- python的retrying模块
- Spring简介
- 29 Three.js的特殊光源THREE.RectAreaLight窗口射入光线模拟
- NYOJ题目18-The Triangle(经典dp)
- 51nod 1280 前缀后缀集合(set)
- CNN(卷积神经网络)、RNN(循环神经网络)、DNN(深度神经网络)的内部网络结构有什么区别?
- Codeforces 842C
- 【半年总结】——化繁为简
- Linux下安装Mysql(yum指令)
- Python实现设计模式--07.原型模式(Prototype Pattern)
- CSS总结——2
- 【HTML5学习笔记】28:CSS3文本效果