POJ 1163 Numeric triangle
Description
Find a path from the top to the bottom in the digital triangle above, so that the sum of the numbers on the path is the largest. Every step on the path can only go down left or down right. You only need to ask for the maximum sum, and you don't need to give a specific path.
The number of rows of the triangle is greater than 1 and less than or equal to 100, and the number is 0-99
Input
Enter the number of rows of digital triangles in the first line, and then enter the number of triangles
Output
Output the largest number sum required by the question
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
AC code
#include <iostream>
#define MAX 101
using namespace std;
int D[MAX][MAX];
int *maxSum;
int i, j, n;
int main()
{
cin >> n;
for(i = 1;i <= n;i++)
for(j = 1;j <= i;j++)
cin >> D[i][j];
maxSum = D[n]; //*maxSum指向第n行
for(i = n-1; i >= 1;i--)//从第n-1行开始向上
for(j = 1; j <= i;j++)
maxSum[j] = max(maxSum[j], maxSum[j+1]) + D[i][j];
//通过用指针不断迭代更新第n行的数实现空间的节省
cout << maxSum[1] << endl;
return 0;
}
The time complexity of the algorithm is o (n 2) o(n^2)o ( n2)