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 &gt; 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的更多相关文章

  1. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  2. The Triangle

    针对如下形式的ACM试题,大多出自南阳理工学院的在线ACM试题(网址: 南阳理工在线评测系统),在此非常感谢,同时也非常感谢作者的分享! 时间限制:1000 ms  |  内存限制:65535 KB ...

  3. 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 ...

  4. poj 1163 The Triangle

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43809   Accepted: 26430 De ...

  5. 【动态规划】The Triangle

    问题 E: [动态规划]The Triangle 时间限制: 1 Sec  内存限制: 128 MB提交: 24  解决: 24[提交][状态][讨论版] 题目描述 73 88 1 02 7 4 44 ...

  6. 【LeetCode】120 - Triangle

    原题:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacen ...

  7. Poj 1163 The Triangle 之解题报告

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42232   Accepted: 25527 Description 7 3 ...

  8. OpenJudge/Poj 1163 The Triangle

    1.链接地址: http://bailian.openjudge.cn/practice/1163 http://poj.org/problem?id=1163 2.题目: 总时间限制: 1000ms ...

  9. 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 ...

随机推荐

  1. [Apple开发者帐户帮助]四、管理密钥(1)创建私钥以访问服务

    私钥允许您访问和验证与某些应用服务(如APN,MusicKit和DeviceCheck)的通信.您将在对该服务的请求中使用JSON Web令牌(JWT)中的私钥. 所需角色:帐户持有人或管理员. 在“ ...

  2. [Apple开发者帐户帮助]三、创建证书(8)撤销证书

    您可以根据证书类型和角色撤消证书.有关详细信息,请转到撤消权限. 要了解撤销证书时会发生什么,请转到Apple Developer支持中的证书. 所需角色:帐户持有人或管理员. 在“ 证书”,“标识符 ...

  3. Elasticserach 同步索引报错:ElasticSearch ClusterBlockException[blocked by: [FORBIDDEN/12/index read-only / allow delete (api)]

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  4. 【专题系列】单调队列优化DP

    Tip:还有很多更有深度的题目,这里不再给出,只给了几道基本的题目(本来想继续更的,但是现在做的题目不是这一块内容,以后有空可能会继续补上) 单调队列——看起来就是很高级的玩意儿,显然是个队列,而且其 ...

  5. jdbc 接口学习笔记

    一.JDBC概念 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系型数据库提供统一访问,它由一组用Jav ...

  6. BZOJ 4525 二分

    思路: 满足二分性质... 二分一下      就完了 //By SiriusRen #include <cstdio> #include <algorithm> using ...

  7. Django学习案例一(blog):二. 连接数据库

    本例使用了django默认的sqlite3数据库,配置文件不需要作调整: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite ...

  8. Redis配置文件各项参数说明及性能调优

    Redis配置文件参数说明: 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no 2. 当Redis以守护进程方式运行时,Redis默 ...

  9. 简繁体互换工具:opencc

    简繁体互换工具:opencc opencc是一个简体.繁体相互转换的命令行工具. 安装 下载软件包.在下载页面下载软件包(如1.0.4版本) 解压.通过命令解压:tar -xzvf opencc-1. ...

  10. JAVA软件工程师应该具备的技能有哪些?

    前言:有朋友问我:学历和能力哪个重要?我个人觉得能力大于学历,没有能力哪来的学历,学历只是证明能力的一方面.为此在能力方面畅谈java软件工程师必备的能力.作为一名合格的java工程师,不仅需要学历, ...