递归的使用

在使用递归时要注意:【1】必须有结束条件 【2】函数公式

练习的程序


//===========用有默认参数值的函数-----设计一个求阶乘的函数

/*
#include<iostream>
using namespace std;
//using recursion

int fun(int m)
    {
     if(m==0||m==1)
         return 1;
     else return fun(m-1)*m;
    
    
    }

// the function have default arge
int func(int m=10)
    {
      int i,s;
      for(i=1,s=1;i<=m;i++)
          s=s*i;
      return s;
    
    
    }
int main()
    {
     int n;
     cout<<"input the number:"<<endl;
     //cin>>n;
     //cout<<"the result is:"<<fun(n)<<endl;
     cout<<"the result2 is:"<<func(n)<<endl;
     cout<<"the result3 is:"<<func()<<endl;//在没有函数实参时,使用默认的形参的值
    return 0;}

  */
//===============================使用递归函数============================
/*
#include<iostream>
using namespace std;
int hmrmite(int x,int n)
    {
     if(n==0)return 1;
     if(n==1)return 2*x;
     else return 2*x*hmrmite(x,n-1)-2*(n-1)*hmrmite(x,n-2);
    }
int main()
    {
     int a,b;
     cout<<"input a,b:"<<endl;
     cin>>a>>b;

     cout<<"the result is:"<<endl;
     cout<<hmrmite(a,b)<<endl;
    return 0;}

*/
//======================递归求阿克曼函数==================================
/*
#include<iostream>
using namespace std;
int acm(int m,int n)
    {if(m==0)return n+1;
     if(n==0)return acm(m-1,1);
     else return acm(m-1,acm(m,n-1));
    }

int main()
    {
     int a,b;
     cout<<"input the two numbers:"<<endl;
     cin>>a>>b;
     cout<<"the result is:"<<endl;
     cout<<acm(a,b)<<endl;
    return 0;}
    */

//============================递归求fibonnaci======================================

#include<iostream>
using namespace std;
int fibo(int n)
    {
     if(n==2||n==1)return 1;
     else return fibo(n-1)+fibo(n-2);
    }
int main()
    {int n1;
     int count;
     cout<<"the number:"<<endl;
     cin>>n1;
     count=n1;
     cout<<"the result is:"<<endl;
     while(count>=1)
         {
            cout<<fibo(n1)<<'\t';
            if(count%5==0)
                 cout<<endl;
            count--;
            n1--;
         }
    return 0;}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值