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. C/C++综合測试题(四)

    又刷了一套题 这些题都是百度.阿里巴巴.腾讯.网易.新浪等公司的面试原题.有一定的难度.只是确实相当有水平.能够通过做题来查漏补缺. 11.以下代码的输出是什么? class A { public: ...

  2. java中关于'&&'、'||'混合运算优先级问题小结

    package com.per.sdg.operator; /** * 结论:先进行'&&'运算,在进行'||'运算 * @author sundg * */ public class ...

  3. HDU Today HDU杭电2112【Dijkstra || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=2112 Problem Description 经过锦囊相助,海东集团最终度过了危机,从此.HDU的发展就一直顺风 ...

  4. npm安装以及命令行

    安装visual studio的时候,安装Node.js会同时安装npm 查看版本 PS C:\Users\clu\Desktop> npm --version5.6.0 PS C:\Users ...

  5. mybatis多表关联配置

    首先需要的jar包 mybatis-3.4.1 配置文件(mybatis.cfg.xml) <?xml version="1.0" encoding="UTF-8& ...

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

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

  7. 杂项-Java:JSP

    ylbtech-杂项-Java:JSP 1.返回顶部 1. JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它是由Sun Micros ...

  8. Mysql中分页查询两个方法比较

    mysql中分页查询有两种方式, 一种是使用COUNT(*)的方式,具体代码如下 1 2 3 SELECT COUNT(*) FROM foo WHERE b = 1;   SELECT a FROM ...

  9. 计算误差——ACM计算几何中的精度问题

    浮点数为何会有精度问题   占字节数 数值范围 十进制精度位数 float 4 -3.4e-38~3.4e38 6~7 double 8 -1.7e-308~1.7e308 14~15 如果内存不是很 ...

  10. 牛客练习赛17-A-长方体

    题目描述 给出共享长方体一个顶点的三个面的面积,求它十二条边的边长和. 输入描述: 一行三个整数a, b, c表示面积(1 <= a, b, c <= 10000). 输出描述: 一行一个 ...