求C++大神帮忙

P1035 [NOIP2002 普及组] 级数求和

陈梓熠 @ 2017-08-14 15:08:46

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

by yylidiw @ 2017-08-14 15:13:19

1改为1.0 @陈梓熠


by Hcf2002 @ 2017-08-14 15:26:25

题目要求输出最小的N

还有你的sn要初始化为0啊


by IamJOE @ 2017-08-16 21:46:28

标程在此!

#include<iostream>
using namespace std;
int main()
{
    int i=1,j,k,n;
    double s=0;
    cin>>n;
    while(s<=n)
    {
        s+=(double)1/i;
        if(s>n)
          break;
        i++;
    }
    cout<<i;
    return 0;
}

by 进过女厕所 @ 2017-08-24 13:07:18

1是1.0


|