13-栈的简单应用-递归

使用递归实现n的阶乘

栈与递归的思想:
一个递归的调用过程为:系统->main->f(4)->f(3)->f(2)->f(1)->f(0),然后从f(0)开始return


#include <iostream>

using namespace std;
int f(int n)
{
    if(n == 0) return 1;
    return n*f(n-1);
}
int main()
{
    cout <<f(4)<< endl;
    return 0;
}

(感谢西交wrong学长提供以上题目练习)

猜你喜欢

转载自blog.csdn.net/outer_star/article/details/82077574
今日推荐