为虾米是60分,求助大佬

P1035 [NOIP2002 普及组] 级数求和

KFC爷爷 @ 2017-11-27 22:35:18

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
     //freopen(".in","r",stdin);
     //freopen(".out","w",stdout);
     float k,s=0.0000,i;
    cin>>k;
    for(i=1;s<=k;i++)
    {
        s+=1.0/i;
    }
    cout<<i-1<<endl;
    return 0;
}

by 小粉兔 @ 2017-11-27 23:51:05

精度不够,用double试试?


by Anguei @ 2017-11-28 00:06:20


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
     //freopen(".in","r",stdin);
     //freopen(".out","w",stdout);
     double k,s=0.0000,i;
    cin>>k;
    for(i=1;s<=k;i++)
    {
        s+=1.0/i;
    }
    cout<<i-1<<endl;
    return 0;
}

by 2017gangbazi @ 2017-12-01 21:14:05

double就好了,我就是float 60分


|