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 ...
随机推荐
- Hardwood Species(map)
http://poj.org/problem?id=2418 题意:给定一系列字符串,要求按字典序升序输出每个串,并输出每个串出现的百分比. 用map做的,交c++A了,G++ WA..so sad. ...
- 0505 php-数组、控制语句、函数
数 组 (定义.使用.赋值.遍历.分类.冒泡排序) 1.数组包括元素.下标.数组长度 2.php中的数组长度用$len = count("$数组名"); 3.定义一个数组:$arr ...
- C/C++中输入多组数据方法
--------开始-------- 对于刚开始学编程的人来说每次基本上就是一次数据输入,多次的话基本也是会给定一个数组的大小,但随着做刷算法题开始,题目有的会不直接告诉输入几组数据,基本输入都是多组 ...
- Java多线程技术-wait/notify/join
wait/notify的作用 wait()方法的作用是使当前执行代码的线程进行等待,wait()是Object类的方法,用来将当前线程置入预执行队列中,并且在wait()所在的代码处停止执行,直到接到 ...
- Java.HttpClient绕过Https证书解决方案一
方案1 import javax.net.ssl.*; import java.io.*; import java.net.URL; import java.security.KeyManagemen ...
- A - Word
Problem description Vasya is very upset that many people on the Net mix uppercase and lowercase lett ...
- RPC与REST
RPC与REST (摘自网络,个人理解)
- Android Studio连接夜神模拟器
运行-cmd,进入夜神模拟器安装目录,进入目录下的bin 执行Nox_adb.exe connect 127.0.0.1:62001命令,connect左右都有空格. 执行命令后就可以连接到夜神模拟器 ...
- crontab与系统时间不一致
将线上数据库迁移至虚拟机后,运维没有把时间修改. 在后期把时间修改完成后,在数据库上也要修改修改,但是定时任务的备份时间却不在凌晨4点执行,而是在中午12:10分执行. 原因是修改时间后,需要重启cr ...
- 2013款MacBook Air装Windows7单系统
经过两天的摸索,查找无数资料终于把2013款的MacBook Air装上了WIN 7,虽然网上有很多的资料但是都不是我想要的,第一个我的是2013款的MacBook Air,跟原来2012 11款Ma ...