一只青蛙一次可以跳上1级台阶,也可以跳上2级,也可以跳3级。求该青蛙跳上一个n级的台阶总共有多少种跳法,并且打印所有跳跃路径。

问题描述:一只青蛙一次可以跳上1级台阶,也可以跳上2级,也可以跳3级。求该青蛙跳上一个n级的台阶总共有多少种跳法,并且打印所有跳跃路径。 

  1.   /** 
  2.     * 跳台阶 
  3.     * @param hasJumped 已经跳过的台阶路径 
  4.     * @param currentStair 当前所在的台阶编号 
  5.     * @param jumpStep 跳跃的台阶数 
  6.     * @param maxStep 青蛙能跳的最大台阶数  
  7.     * @param stairNum 台阶总数 
  8.     */

  #include<vector>  

    #include<string>  
    using namespace std;  
       vector<vector<int>> res;
       vector<int> path;
    /************************************************   
   
     * Declaration: All Rigths Reserved !!!   
     ***********************************************/  
      vector<vector<int>> jumpstair(vector<int> hasjump,int curr,int maxstep,int total)
      {
      if(curr>total)
          return res;
      if(curr==total)
      {
          for(int i=0;i<total;i++)
          {
          if(hasjump[i]>0)
              path.push_back(hasjump[i]);
          }
         res.push_back(path);
         path.clear();
         return res;
    }
    if(curr<total)
    {
        for(int j=maxstep;j>=1;j--)
        {
        int newcurr=curr+j;
        //if(newcurr<=total)
        hasjump[curr]=newcurr;
//         for(int k=curr+1;k<curr+j;k++)//
//         hasjump[k]=0;
        jumpstair(hasjump,newcurr,maxstep,total);
        }
       return res;
        
    }
    }
    
    int main()  
    {  
    int total=7;
    int maxstep=2;
//     int hasjump[5]={0};
    vector<int> hasjump(total);
    
    jumpstair(hasjump,0,maxstep,total);
    cout<<" res"<<res.size()<<endl;
    for(int i=0;i<res.size();i++)
    {
        {
        for(int j=0;j<res[i].size();j++)
        cout<<res[i][j]<<" ";
        }
        cout<<"\n";
    }

        return 0;  
    }  

     


    #include<iostream>  
    #include<vector>  
    #include<string>  
    using namespace std;  
       vector<vector<int>> res;
       vector<int> path;
    /************************************************   
     *
     *
     * Date: 2017-2-17    
     * Declaration: All Rigths Reserved !!!   
     ***********************************************/  
      vector<vector<int>> jumpstair(vector<int> &hasjump,int curr,int maxstep,int total)///必须要引用参数!
      {
      if(curr>total)
          return res;
      if(curr==total)
      {
          for(int i=0;i<total+1;i++)
          {
          if(hasjump[i]>0)
          path.push_back(i);
          }
         res.push_back(path);
         path.clear();
         hasjump[curr]=0;
         return res;
    }
    if(curr<total)
    {
        for(int j=1;j<=maxstep;j++)///从1加到3;;;
        {
        hasjump[curr]=1;
        int newcurr=curr+j;
        if(newcurr<=total)
        hasjump[newcurr]=1;
//         for(int k=curr+1;k<curr+j;k++)//
//         hasjump[k]=0;
        jumpstair(hasjump,newcurr,maxstep,total);
        hasjump[curr]=0;
        }
       return res;
        
    }
    }
    
    int main()  
    {  
    int total=5;
    int maxstep=2;
//     int hasjump[5]={0};
    vector<int> hasjump(total+1);
    
    jumpstair(hasjump,0,maxstep,total);
    cout<<" res"<<res.size()<<endl;
    for(int i=0;i<res.size();i++)
    {
        {
        for(int j=0;j<res[i].size();j++)
        cout<<res[i][j]<<" ";
        }
        cout<<"\n";
    }

        return 0;  
    }  
      


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值