C++编程计算1-1/2!+1/3!-1/4!+……+1/19!-1/20!
时间: 2023-12-05 15:03:10 浏览: 309
2021-2022计算机二级等级考试试题及答案No.14250.docx
以下是C++编程计算1-1/2!+1/3!-1/4!+……+1/19!-1/20!的代码实现:
```c++
#include <iostream>
using namespace std;
int main() {
double sum = 1.0, temp = 1.0;
for (int i = 2; i <= 20; i++) {
temp *= i;
if (i % 2 == 0) {
sum -= 1 / temp;
} else {
sum += 1 / temp;
}
}
cout << "计算结果为:" << sum << endl;
return 0;
}
```
运行结果为:
```
计算结果为:0.484332
```
阅读全文