6581 Number Triangle
6581 Number Triangle
时间限制:500MS 内存限制:1000K
提交次数:57 通过次数:47
题型: 编程题 语言: G++;GCC
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.
输入格式
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
作者
admin
最原始的数塔问题,,经典的动态规划问题。状态转移方程:dp[i][j]=max(dp[i-1][j-1],dp[i-1][j])+a[i][j];
其他细节见代码注释:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std; int a[][];//a[i][i]为数塔上点[i][i]处输入的值
int dp[][];//dp[i][j]为走到点[i][j]所能得的最大值
int main()
{
//freopen("input.txt","r",stdin);
memset(a,,sizeof(a)); //初始化数组
memset(dp,,sizeof(dp));
int n;
scanf("%d",&n); //输入数据
for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
scanf("%d",&a[i][j]);
}
}
//
dp[][]=a[][];
if(n==) //n为1就直接输入塔顶数字
{
printf("%d\n",dp[][]);
return ;
}
//状态转移方程
for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
//走到第a[i][j]位置能得到的最大值dp[i][j]就是等于选择从走到a[i-1][j-1]和走到
//a[i-1][j]中值较大的那种走法里再加上a[i][j];
dp[i][j]=max(dp[i-][j-],dp[i-][j])+a[i][j];
}
}
//
int Max=-;
for(int i=;i<=n;i++)//扫描塔的最下层,找出最大值
if(dp[n][i]>Max)
Max=dp[n][i];
printf("%d\n",Max);
return ;
}
6581 Number Triangle的更多相关文章
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- The Triangle
针对如下形式的ACM试题,大多出自南阳理工学院的在线ACM试题(网址: 南阳理工在线评测系统),在此非常感谢,同时也非常感谢作者的分享! 时间限制:1000 ms | 内存限制:65535 KB ...
- POJ 1163:The Triangle
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 progr ...
- poj 1163 The Triangle
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43809 Accepted: 26430 De ...
- 【动态规划】The Triangle
问题 E: [动态规划]The Triangle 时间限制: 1 Sec 内存限制: 128 MB提交: 24 解决: 24[提交][状态][讨论版] 题目描述 73 88 1 02 7 4 44 ...
- 【LeetCode】120 - Triangle
原题:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacen ...
- Poj 1163 The Triangle 之解题报告
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42232 Accepted: 25527 Description 7 3 ...
- OpenJudge/Poj 1163 The Triangle
1.链接地址: http://bailian.openjudge.cn/practice/1163 http://poj.org/problem?id=1163 2.题目: 总时间限制: 1000ms ...
- POJ1163——The Triangle
Description 73 88 1 02 7 4 44 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program t ...
随机推荐
- Python 38 sql基础
数据库服务器中存放的是 库(文件加) .表(文件) .表里面是记录(一行数据) 增 删 改 查 1.库相关 创建------------------create databa ...
- TCP/IP详解(三)
超时与重传: TCP在发送一个包时,启动一个定时器,如果在定时器溢出之前没有收到ACK,则认为发出的包丢失了,此时会重传丢失的包.这就是超时重传. 其中定时器的时间不是一个固定值,它是根据RTT计算的 ...
- 使用ZeppelinHub来存储和展示ZeppelinNoteBook
0.序 说实在的这个功能太赞了 在一开始接触的时候不知道有这个功能,我尝试做一下配置,发现非常的棒. 棒的原因有两点: 可以在随时随地有互联网的地方访问自己的ZeppelinHub来查看Zeppeli ...
- javascript事件绑定1-模拟jquery可爱的东西
1.给对象添加事件attachEvent(兼容IE,不兼容ff.chrome) <html xmlns="http://www.w3.org/1999/xhtml"> ...
- 实例化vue发生了什么(详解vue生命周期)
const app = new Vue({ el:"#app', data:{ message:'hello,lifePeriod' }, methods:{ init(){ console ...
- ★Java语法(二)——————————数据类型常见问题
1.用float型定义变量:float a = 3.14 :是否正确? 不正确.“=” 两边的精度类型不匹配,应为:float a =(float)3.14 或 float a =3.14F 或 ...
- WEB笔记-2 剖析CSS规则
2.1 剖析CSS规则 规则即指令,其声明了需要修改的元素及要应用给元素的样式. 2.2 为文档添加样式的三种方法 行内样式:直接写在HTML文档标签中的style属性当中,行内元素只 ...
- win7 64位装sql2000
1.运行不了安装程序 右击安装exe文件->属性->兼容性->以xp sp3兼容和管理员身份 2.安装过程中提示“被挂起”的故障 解决:打开注册表编辑器,在HKEY_LOCAL_MA ...
- 转:Fiddler抓包工具总结
http://www.cnblogs.com/yyhh/p/5140852.html#l02
- Windows Phone 应用程序的生命周期(二)
一.App.xaml.cs /// <summary> /// Application 对象的构造函数. /// </summary> public App() { // 未捕 ...