热心网友
回答时间:2024-05-09 21:09
//如果将这个阶乘计算函数以类的形式定义的话,使用如下代码:#include <iostream>
#include <conio.h>using namespace std;//定义阶乘类
class Factorial
{
public:
unsigned long N(const unsigned int n)
{
unsigned long total; if(n == 0)
total = 1;
else
total= ( n - 1) * n; return total;
}
};int main(int argc, char *argv[])
{
//实现类对象
Factorial fact; //计算5!并输出
cout<<fact.N(5)<<endl;
getch();
return 0;
}
收起
热心网友
回答时间:2024-05-09 21:09
int abc(int n)
{
if(n<2)
{
return 1;
}
else
{
return n*abc(n-1);
}
}
收起
热心网友
回答时间:2024-05-09 21:10
#include <iostream>using namespace std;
class Factorial{public:int Count(int n) //运用递归的效率高些 {if(n == 0){return 1;}return n * Count(n - 1);}};
int main(){ Factorial f ; int n = 0;cout << "输入n值:" ; cin >> n;cout << f.Count(n) << endl; // 输出结果return 0;}
收起
热心网友
回答时间:2024-05-09 21:10
用什么语言写 如果是java 的 我可以帮着写下
本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。
收起