分类
Level5

函数递归

函数递归 在一个函数内部,也可以调用函数本身。

#include <iostream> 
using namespace std;
int fact(int n){   //求n的阶乘
    if(n==1 || n==0 ) return 1;  //边界值,n为1或0时,直接返回1
    return n*fact(n-1);  //返回 n * n-1的阶乘
}
int main(){
    int a;
    cin>>a;  //输入正整数n
    
    cout<<fact(a)<<endl;
    return 0;
}