Problem Description: Look for a path from the top to the bottom in the number triangle below, so that the sum of the numbers passing through the path is the largest, and each step on the path can only go down to the left or right. Just ask for this maximum and can, do not have to give a specific path.
Requirements for entering data: The number of rows in the triangle is greater than 1 and is less than or equal to 100, and the number is 0--99.
Input format:
5 //Triangle line number, below is the triangle
7
3 8
8 1 0
2 7 4 4
4 5 2) 6 5
#include <iostream>using namespace Std;const int N = 101;int A[n][n], N;int main () {cin >> n;for (int i = 0; i& Lt;n; i++) {for (int j = 0; j<=i; j + +) {cin >> a[i][j];}} for (int i = n-2; i>=0; i--) {for (int j = 0; j<=i; j + +) {int x = a[i+1][j];if (x < a[i+1][j+1]) x = A[i+1][j+1];a[i ][J] = A[i][j] + x;}} cout << a[0][0]<< Endl;return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Dynamic Programming _ Digital triangles