输入一个正整数n,计算1+1/(1+2)+1/(1+2+3)+·· · ·+1/(1+2+3+··· · ·+n)的值(2 < n < 1000, 结果保留两位小数)。c++
时间: 2024-01-09 09:21:56 浏览: 58
[递归调用]从1+2+3+...+n的值.cpp
以下是C++代码示例,用于计算1+1/(1+2)+1/(1+2+3)+···+1/(1+2+3+···+n)的值:
```cpp
#include <iostream>
using namespace std;
int main() {
int n;
cout << "请输入一个正整数:";
cin >> n;
float sum = 0;
float total = 0;
for (int i = 1; i <= n; i++) {
total += i;
sum += 1 / total;
}
cout << "结果为:" << fixed << setprecision(2) << sum << endl;
return 0;
}
```
阅读全文