修罗王是这样回答的:“我在思索人生的意义,你看这蚂蚁的面前有无数的道路选择,但它不知道选择哪条路可以到达目标,也不知道哪条路上有更多的食物,更不知道现在选择的道路对它以后的影响……”
如图所示,有一个层数为n(n≤1000)的数字三角形。现有一只蚂蚁从顶层开始向下走,每走下一级时,可向左下方向或右下方向走。求走到底层后它所经过数字的总和的最大值。
![技术分享](http://exam.upc.edu.cn/upload/image/20160630/20160630131814_51331.jpg)
标签:
5
1
6 3
8 2 6
2 1 6 5
3 2 4 7 6
23
最大值=1+3+6+6+7=23
#include <iostream> #include <cstdio> #include <cstring> using namespace std; long long int a[1002][1002]; int main() { int n; scanf("%d", &n); for(int i = 1; i <= n; i++){ for(int j = 1; j <= i; j++){ scanf("%lld",&a[i][j]); } } for(int i = n-1; i >=1; i--){ for(int j = 1; j <= i; j++){ a[i][j] += max(a[i+1][j],a[i+1][j+1]); } } printf("%lld\n",a[1][1]); return 0; }
标签:
原文地址:http://www.cnblogs.com/cshg/p/5641849.html