c++实现1/1!+1/(1!+4!)+1/(1!+4!+7!)+1/(1!+4!+7!+10!)+1/(1!+4!+7!+10!+13!)+1/(1!+4!+7!+10!+13!+16!)+1/(1!+4!+7!+10!+13!+16!+19!)
时间: 2023-11-21 15:05:28 浏览: 77
c++ 1!+2!+3!+..
以下是C++实现1/1!+1/(1!+4!)+1/(1!+4!+7!)+1/(1!+4!+7!+10!)+1/(1!+4!+7!+10!+13!)+1/(1!+4!+7!+10!+13!+16!)+1/(1!+4!+7!+10!+13!+16!+19!)的代码和注释:
```c++
#include <iostream>
using namespace std;
int main() {
int n = 7; // n为7,即求前7项的和
double sum = 0; // 初始化和为0
double factorial = 1; // 阶乘的初始值为1
double denominator = 1; // 分母的初始值为1
for (int i = 1; i <= n; i++) { // 循环n次,求前n项的和
factorial *= i; // 计算i的阶乘
denominator += 3; // 分母每次加3
sum += 1 / (factorial * denominator); // 计算当前项的值并加到和上
}
sum += 1; // 最后一项的分母为22,加上1
cout << "Sum = " << sum << endl; // 输出结果
return 0;
}
```
阅读全文