求助 为什么WA

P1035 [NOIP2002 普及组] 级数求和

TsAIwc @ 2022-07-10 09:04:51

#include<bits/stdc++.h>
using namespace std;
int k;
int n=1;
float Sn=0;
int main() 
{
    cin>>k;

    while(Sn<=k)
        Sn+=1.0/n;
        n++;

    cout<<n;
    return 0;
}

感觉这样也可以

就是一直WA

求解答


by TeraniRetZiger @ 2022-07-10 09:06:51

@TsAIwc float改成double


by TsAIwc @ 2022-07-10 09:09:21

@Static_int 改了 还是不行


by TsAIwc @ 2022-07-10 09:10:15

啊while后面掉了个括号我加上了,然后还是不行


by TsAIwc @ 2022-07-10 09:15:41

看其他帖子说输出结果要减1...?我试了还不对,而且为什么要减一


by TeraniRetZiger @ 2022-07-10 09:20:36

@TsAIwc

#include<bits/stdc++.h>

using namespace std;

int k, n;

double Sn = 0;

int main() {
    cin >> k;
    while (Sn <= k) n++, Sn += (double)1 / n;
    cout << n;
}

by TeraniRetZiger @ 2022-07-10 09:20:58

1 / n也要double


by shoot_down @ 2022-07-10 09:22:29

@TsAIwc 把所有的int改成double


by TsAIwc @ 2022-07-10 09:26:37

@Static_int 好了!感谢


by TsAIwc @ 2022-07-10 09:27:02

@20200900193lrq 已经解决了 谢谢


|