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 ;
}

最新文章

  1. SPOJ : DIVCNT2 - Counting Divisors (square)
  2. 利用ARCHPR明文破解获取PDF
  3. DNS(一)之禁用权威域名服务器递归解析
  4. javascript [] 与 {} 的区别
  5. 怎样在Eclipse中使用debug模式调试程序
  6. 创建第一个Android应用程序 HelloWorld
  7. Linux之定时任务
  8. Git配合Tag的代码回滚
  9. Docker系列之swarm集群搭建
  10. BurpSuite工具抓取手机的流量
  11. ESB简介及选型(转) http://www.cnblogs.com/skyme/archive/2012/08/06/2623414.html
  12. .net中 登录 才能下载文件的方法 Response.WriteFile实现下载
  13. 用java开发图形界面项目,如何实现从本地选择图片文件并以二进制流的形式保存到MySQL数据库,并重新现实到面板
  14. 必须知道的Spring Boot中的一些Controller注解
  15. Derek解读Bytom源码-P2P网络 地址簿
  16. java代码中实现android背景选择的selector-StateListDrawable的应用
  17. Thinkphp5 微信公众号token验证不成功的原因
  18. PostgreSQL误删操作怎么处理
  19. 3D游戏与计算机图形学中的数学方法-点线面
  20. “全栈2019”Java多线程第七章:等待线程死亡join()方法详解

热门文章

  1. YII数据流程浅析
  2. jQuery -&amp;gt; 获取指定上下文中的DOM元素
  3. 05-图1. List Components (25)
  4. android XXXActivity和getApplicationContext()差别
  5. build.gradle解析
  6. Run bash script as daemon
  7. Oracle学习(四):组函数
  8. ios26--kvc
  9. 【CEOI2002】【Poj 1038】Bugs Integrated, Inc.
  10. 5. extjs 中buttonAlign什么意思