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.
样例输入
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
样例输出
30
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
int n,maxx=0,num[105][105]= {0};
scanf("%d",&n);
for(int i=1; i<=n; i++)
for(int j=1; j<=i; j++)
scanf("%d",&num[i][j]);
for(int i=2; i<=n; i++)
{
for(int j=1; j<=i; j++)
{
num[i][j]=num[i][j]+max(num[i-1][j],num[i-1][j-1]);
}
}
for(int i=1; i<=n; i++)
if(num[n][i]>maxx)
maxx=num[n][i];
printf("%d\n",maxx);
return 0;
}
//#include<stdio.h>
//#include<string.h>
//#include<algorithm>
//using namespace std;
//
//int n,dp[110][110],map[110][110];
//
//int dfs(int i,int j)
//{
// if(dp[i][j]!=-1)
// return dp[i][j];
// if(i==n) dp[i][j]=map[i][j];
// else
// {
// int x=dfs(i+1,j);
// int y=dfs(i+1,j+1);
// dp[i][j]=max(x,y)+map[i][j];
// }
// return dp[i][j];
//}
//int main()
//{
// int i,j;
// scanf("%d",&n);
// for(i=1; i<=n; i++)
// {
// for(j=1; j<=i; j++)
// {
// scanf("%d",&map[i][j]);
// dp[i][j]=-1;
// }
// }
// printf("%d\n",dfs(1,1));
// return 0;
//} //#include <stdio.h>
//#include <algorithm>
//using namespace std;
//int main()
//{
// int n,num[105][105]= {0};
// scanf("%d",&n);
// for(int i=1; i<=n; i++)
// for(int j=1; j<=i; j++)
// scanf("%d",&num[i][j]);
// for(int i=n-1; i>=0; i--)
// for(int j=1; j<=i; j++)
// num[i][j]=num[i][j]+max(num[i+1][j],num[i+1][j+1]);
// printf("%d\n",num[1][1]);
// return 0;
//}
//
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; int n;
int dp[110][110],map[110][110]; int d(int i,int j)
{
if(dp[i][j]>=0) return dp[i][j];
return dp[i][j]=map[i][j]+(i==n?0:(d(i+1,j)>d(i+1,j+1)?d(i+1,j):d(i+1,j+1)));
} int main()
{
while(~scanf("%d",&n))
{
int i,j;
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
{
scanf("%d",&map[i][j]);
dp[i][j]=-1;
}
}
int ans=d(1,1);
printf("%d\n",ans);
}
return 0;
}

nyoj 18 The Triangle的更多相关文章

  1. NYOJ 18 The Triangle 填表法,普通dp

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=18 The Triangle 时间限制:1000 ms  |  内存限制:6553 ...

  2. leetcode 数组array

    120. Triangle 给出一个三角形(数据数组),找出从上往下的最小路径和.每一步只能移动到下一行中的相邻结点上. 解法,自底向上 The idea is simple. Go from bot ...

  3. acdream.18.KIDx's Triangle(数学推导)

    KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Sub ...

  4. nyoj 18-The Triangle(动态规划)

    18-The Triangle 内存限制:64MB 时间限制:1000ms Special Judge: No accepted:5 submit:5 题目描述: 7 3 8 8 1 0 2 7 4 ...

  5. NYOJ-102 次方求模 AC 分类: NYOJ 2014-02-06 18:53 184人阅读 评论(0) 收藏

    地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=102 //a^b mod c=(a mod c)^b mod c很容易设计出一个基于二分的递归 ...

  6. NYOJ-20 吝啬的国度 AC 分类: NYOJ 2014-01-23 12:18 200人阅读 评论(0) 收藏

    #include<cstdio> #include<cstring> #include<vector> using namespace std; int pre[1 ...

  7. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  8. 设计一个程序,程序中有三个类,Triangle,Lader,Circle。

    //此程序写出三个类,triangle,lader,circle:其中triangle类具有类型为double的a,b,c边以及周长,面积属性, //具有周长,面积以及修改三边的功能,还有判断能否构成 ...

  9. Think Python - Chapter 18 - Inheritance

    In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...

随机推荐

  1. 从零搭建SSM框架(三)SSM框架整合

    整合思路 1.Dao层: Mybatis的配置文件:SqlMapConfig.xml 不需要配置任何内容,需要有文件头.文件必须存在. applicationContext-dao.xml: myba ...

  2. layui利用jQuery设置下拉列表的值

    今天在利用jQuery动态设置下拉列表的值的时候确怎么也赋值不上去,其中用到了layui框架,源代码如下: $.post(contextPath+'/courseLibrary/getCourseBa ...

  3. weblogic nmap扫描脚本

     CVE-2018-2894 / Nmap利用脚本,可批量批量快速扫描getshell.检测漏洞.利用漏洞 地址:https://github.com/Rvn0xsy/nse_vuln/tree/ma ...

  4. 5-python的封装与结构 - set集合

    目录 1 封装与解构 1.1 封装 1.2 解构 1.3 Python3的解构 2 set类型 2.1 set的定义 2.2 set的基本操作 2.2.1 增加元素 2.2.2 删除元素 2.2.3 ...

  5. 【读书笔记::深入理解linux内核】内存寻址

    我对linux高端内存的错误理解都是从这篇文章得来的,这篇文章里讲的 物理地址 = 逻辑地址 – 0xC0000000:这是内核地址空间的地址转换关系. 这句话瞬间让我惊呆了,根据我的CPU的知识,开 ...

  6. rocketmq 记

    Rocketmq选型 Rocket是一个专业的队列服务,性能优于Rabbitmq,优势是性能和并发,源于Kafka的扩展版,增强了数据的可靠性. Rocketmq的队列类型 普通队列,广播队列.顺序队 ...

  7. java并发容器

    同步容器将所有对容器状态的访问都串行化,以实现线程安全性.这种方式的缺点是严重降低并发性.Java 5.0提供了多种并发容器来改进同步容器的性能.如ConcurrentHashMap代替同步且基于散列 ...

  8. Mybatis的初步使用

    MyBatis 是当下最流行的持久层框架,也是ORM框架,本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google ...

  9. DroidParts 中文系列教程(基于官方教程)

    DroidParts中文系列教程(基于官方教程) (一)DroidParts框架概况 2014年4月18日星期五 11:36 他是一个精心构造的安卓框架,包括下面这些基本功能 DI依赖注入,可以注入V ...

  10. @PathVariable @RequestParam @RequestBody 的区别

    转载自:@RequestParam @RequestBody @PathVariable 等参数绑定注解详解 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...