c++求1+1/2+1/3+...+1/n
时间: 2023-04-25 19:02:26 浏览: 159
C++求1+1/2+1/3+...+1/n的代码如下:
```
#include <iostream>
using namespace std;
int main()
{
int n;
double sum = 0;
cout << "请输入n的值:";
cin >> n;
for (int i = 1; i <= n; i++)
sum += 1.0 / i;
cout << "结果为:" << sum << endl;
return 0;
}
```
阅读全文