为啥WA了??

P1035 [NOIP2002 普及组] 级数求和

limuchudsb @ 2024-08-05 18:35:00


#include<bits/stdc++.h>
using namespace std;
int main()
{
    double n,sum=0;
    cin>>n;
    for(double i=1;i<=1000000;i++)
    {
        sum=sum+1/i;
        if(sum>=n)
        {
            cout<<i;
            return 0;
        }
    }
    return 0;
}

by limuchudsb @ 2024-08-05 18:38:36

QAQ


by ___Segment___ @ 2024-08-05 18:41:50

代码第10行有问题。题目中说是大于,不是大于等于。

亲测AC。


by limuchudsb @ 2024-08-05 21:12:43

@chenrongqing 谢谢 ,已关注


by guoyanwei120223 @ 2024-08-13 17:16:59

这样写

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int k,n,cnt=0;
    cin>>k;
    for(double sn=0;sn<=k;cnt++,sn+=1.0/cnt);
    cout<<cnt;
    return 0;
}

求关


|