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. Parencodings(模拟)

    ZOJ Problem Set - 1016 Parencodings Time Limit: 2 Seconds      Memory Limit: 65536 KB Let S = s1 s2 ...

  2. 【NOIP】普及组2010 三国游戏

    [算法]贪心 [题解]如果看重一对,先选择其中一个点,该点相邻最大的肯定被选走.所以答案就是最大的[所有点的次大连边点]啦. #include<cstdio> #include<al ...

  3. 【洛谷 P1501】 [国家集训队]Tree II(LCT)

    题目链接 Tree Ⅱ\(=\)[模板]LCT+[模板]线段树2.. 分别维护3个标记,乘的时候要把加法标记也乘上. 还有就是模数的平方刚好爆\(int\),所以开昂赛德\(int\)就可以了. 我把 ...

  4. VC进度条的使用

    m_progress->GetPos(); //获取进度条的当前位置 m_progress->GetRange(int min,int max); //获取进度条控件的范围的下限和上限 m ...

  5. python 第二章 对象与类型

    可变对象和不可变对象 1,可变对象,list(列表),dict(字典),集合(set),字节数组. 2,不可变对象,数值类型,字符串,字节串,元组(具体形式 ()). 注意条件:可变和不可变指的是该对 ...

  6. JSON.parse()——json字符串转JS

    JSON 通常用于与服务端交换数据. 在接收服务器数据时一般是字符串. 我们可以使用 JSON.parse() 方法将数据转换为 JavaScript 对象. 语法 JSON.parse(text[, ...

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

  8. ACM ICPC Kharagpur Regional 2017

    ACM ICPC Kharagpur Regional 2017 A - Science Fair 题目描述:给定一个有\(n\)个点,\(m\)条无向边的图,其中某两个点记为\(S, T\),另外标 ...

  9. ubuntu16.04 安装 python3.6, 并创建虚拟环境(使用python3.6)

    ubuntu16.04 安装 python3.6, 并创建虚拟环境(使用python3.6) ubuntu16.04中默认安装了 python2.7 python3 python3.5.2 (注意 : ...

  10. java基础14 多态(及关键字:instanceof)

    面向对象的三大特征: 1.封装   (将一类属性封装起来,并提供set()和get()方法给其他对象设置和获取值.或者是将一个运算方法封装起来,其他对象需要此种做运算时,给此对象调用) 2.继承   ...