The Triangle
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 40079   Accepted: 24144

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.

Input

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.

Output

Your program is to write to standard output. The highest sum is written as an integer.

Sample Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Sample Output

30
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXN 105
using namespace std; int tri[MAXN][MAXN];
int sum[MAXN][MAXN]; int main()
{
int i,j;
int n,maxsum=;
cin>>n;
for(i=;i<=n;i++)
for(j=;j<=i;j++)
scanf("%d",&tri[i][j]);
memset(sum,,sizeof(sum));
for(i=;i<=n;i++)
for(j=;j<=i;j++)
{
if(i==)
sum[i][j]=tri[i][j];
else if(j==)
sum[i][j]=sum[i-][j]+tri[i][j];
else if(j==i)
sum[i][j]=sum[i-][j-]+tri[i][j];
else if(j<i)
sum[i][j]=max(sum[i-][j-]+tri[i][j],sum[i-][j]+tri[i][j]);
}
for(i=;i<=n;i++)
maxsum=max(maxsum,sum[n][i]);
cout<<maxsum<<endl;
return ;
}

POJ_1163_The triangle的更多相关文章

  1. [LeetCode] Triangle 三角形

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  2. [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, ...

  3. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  4. 【leetcode】Pascal's Triangle II

    题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...

  5. 【leetcode】Pascal's Triangle

    题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

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

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

  7. Triangle - Delaunay Triangulator

    Triangle - Delaunay Triangulator  eryar@163.com Abstract. Triangle is a 2D quality mesh generator an ...

  8. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  9. LeetCode 119 Pascal's Triangle II

    Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...

随机推荐

  1. 搭建wamp php环境

    点击下载wamp,wamp是一个集成环境,在安装过程中,我们要选择默认的浏览器以及默认的文本编辑器,安装步骤如下: 第一步,选择默认的浏览器(填写默认的浏览器可执行路径就行) 第二步,选择默认的文本编 ...

  2. react 执行 yarn build 页面无法显示

    资源文件路径问题 如果你使用create-react-app创建项目,执行命令 yarn build 后,直接以静态方式打开build文件夹内的index.html,会看到页面显示出现问题,打开con ...

  3. MFC自己主动获取网络地址函数实现----广播地址,网关,子网掩码

    void CSetSignalBoxDlg::OnBnClickedButtonGetbroadcastaddr() {       //凝视部分为还有一种获取IP方式,可略过 //char Name ...

  4. fused multiply and add

    1 要解决的问题 计算x*y + z?其中x.y.z都是浮点数. 2 普通的计算方式 e=3; s=4.734612 × e=5; s=5.417242 ----------------------- ...

  5. LAMP安装问题【已解决】

    1.编译不通过提示cannot find mysql header files under /usr/local/mysql解决办法:修改./configuer  --with-mysql=/usr/ ...

  6. 假如Java对象是个人······

    假如Java对象是个人,那意味着它也具备了我们人所有的东西,头,身体,大长腿. 头 头就是我们的对象头(Header).根据JAVA虚拟机规范,我们的对象头分为两部分,分别是存储对象自身的运行时数据和 ...

  7. BZOJ_3171_[Tjoi2013]循环格_最小费用最大流

    BZOJ_3171_[Tjoi2013]循环格_最小费用最大流 Description 一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子.每个元素有一个坐标(行,列),其中左上角元素坐标为 ...

  8. 【POJ 1201】 Intervals

    [题目链接] 点击打开链接 [算法] 令sum(n)表示区间[1,n]中选了几个点 那么,显然有以下不等式 : 1. sum(n)- sum(n - 1) >= 0 2. sum(n) -  s ...

  9. nginx开发(一) 源码-编译

    1:获取源码 http://nginx.org/download/nginx-1.8.0.tar.gz 2:编译 解压之后,进入根目录,执行 ./configuer.sh make make inst ...

  10. ASP.NET面试点汇总

    1.维护数据库的完整性.一致性.你喜欢用触发器还是自写业务逻辑?为什么答:尽可能用约束(包括CHECK.主键.唯一键.外键.非空字段)实现,这种方式的效率最好:其次用触发器,这种方式可以保证无论何种业 ...