c++编写一个求n!的函数,并调用s=1/1!+1/2!+1/3!+……+1/n!和sum=n!/(n-m)!×m!)+(n>m)
时间: 2023-11-27 20:05:21 浏览: 100
c代码-编写一个求阶乘的函数Fact(),然后在主函数中,计算并输出1到n之间所有数的阶乘值
5星 · 资源好评率100%
以下是C++编写一个求n!的函数,并调用s=1/1!+1/2!+1/3!+……+1/n!和sum=n!/(n-m)!×m!)+(n>m)的例子:
```c++
#include<iostream>
using namespace std;
// 求n的阶乘
int factorial(int n){
if(n == 0 || n == 1){
return 1;
}
else{
return n * factorial(n-1);
}
}
int main(){
int n = 5, m = 2;
double s = 0, sum = 0;
for(int i = 1; i <= n; i++){
s += 1.0 / factorial(i);
}
sum = factorial(n) / (factorial(n-m) * factorial(m));
cout << "s = " << s << endl;
cout << "sum = " << sum << endl;
return 0;
}
```
阅读全文