为什么错了,求大犇

P1035 [NOIP2002 普及组] 级数求和

xqlxql @ 2017-02-27 17:14:15

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{  double s=0,i;long long n;
   cin>>n;
   for(i=1;;i++)
     {
     if(s<n)s+=1.0/i;else  break;}cout<<i<<endl;
     return 0;}

by qwert1996 @ 2017-02-27 20:25:34

(s<=n) i这里是上记录下一个


by 御·Dragon @ 2017-03-01 14:18:45

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int k,i=0;
    double s=0.0000;
    cin>>k;
    do
    {
        i++;
        s=s+ (1.0/i);
    } while (s<=k);
    printf("%d",i);
    return 0;
}

by best @ 2017-03-12 17:21:19

把最后的i改为i-1;

这是while和for循环的区别;

因为for中i取到值后会再执行一遍i++


by 野生小卒 @ 2017-03-12 19:06:33

首先你引用辣么多头文件干毛


by qingwind @ 2017-03-12 19:37:09

#include <iostream>
using namespace std;
int main()
{
    double s = 0, i; long long n;
    cin >> n;
    for (i = 1;; i++)
    { 
                //把你原来改成下面这个
                s += 1.0 / i; 
        if (s<=n) //你原来s<n也错误。
            continue;
        else 
            break;
    }
    cout << i << endl;
    return 0;
}

by lianglexuan2 @ 2017-03-19 10:37:33

#include<iostream>
using namespace std;
int k,c;
double v;
int main()
{
    cin>>k;v=0;c=0;
    while(v<=k)
     {
         c++;
         v=v+(1.0/c);
     }
    cout<<c;
    return 0;
}

by 浩子enjoy @ 2017-03-21 20:17:37

#include<iostream>
using namespace std;
int main()
{
    double sn;
    int k,n;
    cin>>k;
    sn=0.0;
    n=1;
    while(sn<=k) 
    {
        sn=sn+1.0/n;
        ++n;
    }
    cout<<n-1;
    return 0;
}

|