C++编程计算1-1/2!+1/3!
时间: 2023-12-04 15:06:27 浏览: 99
c++ 1!+2!+3!+..
以下是C++编程计算1-1/2!+1/3!的代码:
```c++
#include <iostream>
using namespace std;
int main() {
int n;
float sum = 1, f = 1;
cout << "请输入一个整数n:";
cin >> n;
for (int i = 2; i <= n; i++) {
f *= i;
if (i % 2 == 0) {
sum -= 1 / f;
} else {
sum += 1 / f;
}
}
cout << "sum=" << sum << endl;
return 0;
}
```
阅读全文