标签:triangle where ble pen The 程序设计 define page output
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
(Figure 1)
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
30
/* http://bailian.openjudge.cn/practice/1163/ 1163:The Triangle 递归解法2:数字三角形的记忆递归型动归程序 */ #include<iostream> #include<algorithm> #define MAX 101 using namespace std; int D[MAX][MAX]; int sum[MAX][MAX]; int n; int MaxSum(int i, int j) { if (sum[i][j] != -1)/*说明这个路径的最大和已经算过了*/ { return sum[i][j]; } if (i == n) { sum[i][j] = D[i][j]; } else { int x = MaxSum(i + 1, j); int y = MaxSum(i + 1, j + 1); sum[i][j] = max(x, y) + D[i][j]; } return sum[i][j]; } int main() { int i, j; cin >> n; for (i = 0; i < n; i++) { for (j = 0; j <= i; j++) { cin >> D[i][j]; sum[i][j] = -1; } } cout << MaxSum(0, 0) << endl; return 0; }
标签:triangle where ble pen The 程序设计 define page output
原文地址:https://www.cnblogs.com/focus-z/p/11610310.html