数字三角形问题

TimeLimit: 1000MS Memory Limit: 65536KB

SubmitStatistic

Problem Description

给定一个由n行数字组成的数字三角形如下图所示。试设计一个算法,计算出从三角形的顶至底的一条路径,使该路径经过的数字总和最大。
 

对于给定的由n行数字组成的数字三角形,计算从三角形的顶至底的路径经过的数字和的最大值。

Input

输入数据的第1行是数字三角形的行数n1≤n≤100。接下来n行是数字三角形各行中的数字。所有数字在0..99之间。

Output

输出数据只有一个整数,表示计算出的最大值。

Example Input

5

7

3 8

8 1 0

2 7 4 4

4 5 2 65

Example Output

30

Hint

 

Author

#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
int main()
{
    int n,a[120][120];
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    int t;
    for(int i=n-1;i>0;i--)
    {
        for(int j=1;j<=i;j++)
        {
            a[i][j]+=max(a[i+1][j],a[i+1][j+1]);
        }

    }
    printf("%d\n",a[1][1]);
    return 0;
}



/***************************************************
User name: jk160505徐红博
Result: Accepted
Take time: 0ms
Take Memory: 200KB
Submit time: 2017-01-12 08:37:06
****************************************************/