级数求和哪里错了呀 麻烦大家帮我看一下!

P1035 [NOIP2002 普及组] 级数求和

hunandaxue @ 2023-07-13 15:47:14

#include<iostream>

#include<cstdio>

using namespace std;

int main() {

double s=0;

int n=1;

int k;

cin>>k;

while(s<=k)

{
s=s+1/n;

n++;

}

cout<<n;

return 0; }

by 0x282e202e2029 @ 2023-07-13 15:51:14

```cpp #include <iostream> using namespace std; int main() { int k,n = 0 ; cin >> k ; double Sn = 0.; while(Sn <= k) { n++; Sn += (1. / n); } cout << n << endl; return 0; } ```

by Kong__Bai @ 2023-07-15 00:31:47

s是double类型,n是整形(int)的,1/n是int类型的,所以你的s数据是错误的,应该是s+=1.0/n.


|