数字三角形问题
数字三角形问题
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
给定一个由n行数字组成的数字三角形如下图所示。试设计一个算法,计算出从三角形的顶至底的一条路径,使该路径经过的数字总和最大。
对于给定的由n行数字组成的数字三角形,计算从三角形的顶至底的路径经过的数字和的最大值。
Input
输入数据的第1行是数字三角形的行数n,1≤n≤100。接下来n行是数字三角形各行中的数字。所有数字在0…99之间。
Output
输出数据只有一个整数,表示计算出的最大值。
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
Hint
Source
#include<bits/stdc++.h>
using namespace std;
#define N 150
#define M 20110
#define inf 0x3f3f3f3f
int main()
{
int n, imax = 0;
cin >> n;
int a[N][N] = {0};
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= i; j++)
{
cin >> a[i][j];
//求出来每一层的最大值
a[i][j] += max(a[i-1][j], a[i-1][j-1]);
imax = max(imax, a[i][j]);
}
}
cout << imax << endl;
return 0;
}
1万+

被折叠的 条评论
为什么被折叠?



