the operation of recursion

I've learned about recursion for some time.In short ,it's a function or way that operates by calling itself again and again. But I actually know how it operates today.Take  factorial for example:

#include <iostream>

using namespace std;

int factorial(int n)

int mian()

{

    int x;

    cin>>x;

    cout<<factorial(x)<<endl;

    return 0;

}

int factorial(int n)

{

    if (n==0||n==1) return 1;

    if(n<0) return 0;

    return factorial(n-1)*n;

}

If you input number 3,it will firstly enter factorial(3),while in factorial(3) it has factorial(2) and in factorial(2) it has factorial(1).Until factorial(1) it return 1and 1 will be maintain ed in a temporary variable .Then it will return to factorial(2) and multiply the last temporary variable .The same as factorial(3).

It includes the operation of stack,FILO(first-in ,last-out).



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值