动态规划入门案例代码_跳台阶

1.

devc++调字体

devc++调中文

ctrl+/ 注释或者取消注释

代码块+tab 向右缩进

代码块+shift+tab 向左缩进

2.动态规划入门案例

// #include<bits/stdc++.h>
// using namespace std;
// int fbnq(int n){
//     if(n==1)return 1;
//     if(n==2)return 2;
//     return fbnq(n-1)+fbnq(n-2);
// }
// int main(){
//     int n;
//     scanf("%d",&n);
//     printf("%d",fbnq(n));
//     return 0;
// }

// #include<bits/stdc++.h>
// using namespace std;
// const int N=100;
// int men[N];//记忆数组
// int fbnq(int n){
//     if(men[n])return men[n];
    
//     int sum=0;
//     if(n==1) sum=1;
//     else if(n==2) sum=2;
//     else sum=fbnq(n-1)+fbnq(n-2);
    
//     men[n]=sum;
//     return sum;
// }
// int main(){
//     int n;
//     scanf("%d",&n);
//     printf("%d",fbnq(n));
//     return 0;
// }

// #include<bits/stdc++.h>
// using namespace std;
// const int N=100;
// int f[N];

// int main(){
//     int n;
//     f[1]=1,f[2]=2;
//     scanf("%d",&n);
    
//     if(n==1||n==2){
//         printf("%d",f[n]);
//         return 0;
//     }
    
//     //用空间换时间
//     for(int i=3;i<=n;i++){
//         f[i]=f[i-1]+f[i-2];//dfs的状态转移方程 1:1
//     }
//     printf("%d",f[n]);
//     return 0;
// }

#include<bits/stdc++.h>
using namespace std;

int main(){
    int n;

    scanf("%d",&n);
    
    if(n==1||n==2){
        printf("%d",n);
        return 0;
    }
    
    int newf,top1=1,top2=2;
    for(int i=3;i<=n;i++){
        newf=top2+top1;
        top1=top2;
        top2=newf;//类似于滚动数组
    }
    printf("%d",newf);
    return 0;
}

AcWing题库跳转

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值