数字三角形 动态规划 由下至上

数字三角形 , 就 从上至下的 和最大是几 ? 可以采用从下到上 用贪心的方法 算出来  最大值 下面附上代码 

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<vector>
 8 #include<set>
 9 #include<stack>
10 #include<string>
11 #include<sstream>
12 #include<map>
13 #include<cctype>
14 #include<limits.h>
15 using namespace std;
16 int main()
17 {
18     int sum,a[5055],t,n;
19     while(scanf("%d",&n)!=EOF)
20     {
21         sum=n*(n+1)/2;
22         for(int i=1;i<=sum;i++)
23             scanf("%d",&a[i]);
24         int count1=1;
25         for(int i=sum,total=sum;i-n>0;i--,total--)
26         {
27             a[i-n]=max(a[i-n]+a[total],a[i-n]+a[total-1]);
28             count1++;
29             if(count1==n)
30             {
31                 count1=1;
32                 n--;
33                 i--;
34                 total--;
35             }
36         }
37         printf("%d\n",a[1]);
38     }
39     return 0;
40 }

 

转载于:https://www.cnblogs.com/A-FM/p/5439928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值