【HDUOJ】几道递推DP

就不写题解了。很基础的递推。


 

 

HDU2084数塔

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084

 

代码:

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 
 6 using namespace std;
 7 const int maxn = 105;
 8 
 9 int dp[maxn][maxn];
10 int a[maxn][maxn];
11 
12 int main(){
13     int T;
14     cin>>T;
15     while(T--){
16         memset(a,0,sizeof(a));
17         memset(dp,0,sizeof(dp));
18         int n;
19         cin>>n;
20         for(int i = 1; i <= n ;i++){
21             for(int j = 1; j <= i; j++){
22                 cin>>a[i][j];
23             }
24         }
25 
26         for(int i = n; i > 0; i--){
27             for(int j = 1; j <= n ;j++){
28                 dp[i][j] = max(dp[i+1][j] ,dp[i+1][j+1]) + a[i][j];
29             }
30         }
31         cout<<dp[1][1]<<endl;
32     }
33 
34 
35     return 0;
36 }

 

 


 

 

 

HDU2018母牛的故事

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2018

 

代码:

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 
 6 using namespace std;
 7 const int maxn = 105;
 8 int dp[maxn];
 9 
10 void init(){
11     dp[1] = 1;
12     dp[2] = 2;
13     dp[3] = 3;
14     dp[4] = 4;
15     dp[5] = 6;
16     for(int i = 6; i <= 55; i++){
17         dp[i] = dp[i-1] + dp[i-3];
18     }
19 }
20 
21 int main(){
22     int n;
23     init();
24     while(cin>>n && n){
25         cout<<dp[n]<<endl;
26     }
27 
28     return 0;
29 }

 

 


 

 

 

HDU2044小蜜蜂的故事

同类型HDU2041

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2044

 

代码:

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #define ll long long
 6 using namespace std;
 7 const int maxn = 55;
 8 ll fib[maxn];
 9 
10 void init(){
11     fib[0] = 0;
12     fib[1] = 1;
13     fib[2] = 2;
14     for(int i = 3 ;i <= maxn ;i++){
15         fib[i] = fib[i-1] + fib[i-2];
16     }
17 }
18 
19 int main(){
20     init();
21     int n,m;
22     int T;
23     cin>>T;
24     while(T--){
25         cin>>n>>m;
26         cout<<fib[m-n]<<endl;
27     }
28 
29 
30     return 0;
31 }

 

 


 

 

 

HDU2050

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2050

 

代码:

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #define ll long long
 6 using namespace std;
 7 const int maxn = 10005;
 8 ll dp[maxn];
 9 
10 void init(){
11     dp[1] = 2;
12     dp[2] = 7;
13     for(int i = 3; i <= maxn; i++){
14         dp[i] = dp[i-1] + 4*(i-1) + 1;
15     }
16 }
17 
18 int main(){
19     int T;
20     cin>>T;
21     int n;
22     init();
23     while(T--){
24         cin>>n;
25         cout<<dp[n]<<endl;
26     }
27 
28     return 0;
29 }

 

转载于:https://www.cnblogs.com/Asumi/p/9769934.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值