求1+1/3+1/5+
时间: 2024-01-28 15:03:24 浏览: 96
多项式求和1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ...
5星 · 资源好评率100%
以下是两种求解1+1/3+1/5+...的方法:
方法一:Python代码
```python
n = int(input("请输入n的值:"))
sum = 0
for i in range(1, n+1):
if i % 2 == 1:
sum += 1/i
print("1+1/3+1/5+...的前", n, "项之和为:%.3f" % sum)
```
方法二:C++代码
```c++
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n;
double sum = 0, x;
cout << "Enter n: ";
cin >> n;
for (int i = 1; i <= n; i++) {
x = 2 * i - 1;
sum += 1 / x;
}
cout.setf(ios::fixed);
cout << setprecision(3) << "1+1/3+1/5+...的前" << n << "项之和为:" << sum << endl;
return 0;
}
```
阅读全文