// 用fac函数求n的阶乘
#include<iostream>
#include<cmath>
int fac(int t)
{
int s;
if(t==1)
s=1;
else
{
s=t*fac(t-1);
}
return s;
}
using namespace std;
int main()
{
int n;
cin>>n;
cout<<fac(n)<<endl;
return 0;
}
#include<iostream>
#include<cmath>
int fac(int t)
{
int s;
if(t==1)
s=1;
else
{
s=t*fac(t-1);
}
return s;
}
using namespace std;
int main()
{
int n;
cin>>n;
cout<<fac(n)<<endl;
return 0;
}
本文介绍了一个使用C++编写的递归函数,用于计算任意正整数的阶乘。通过定义fac函数并利用递归调用的方式实现,程序首先接收用户输入的一个整数n,然后输出n的阶乘结果。
1万+

被折叠的 条评论
为什么被折叠?



