为什么时间会超过限制啊???!求指教!

P1035 [NOIP2002 普及组] 级数求和

淼杪 @ 2017-04-09 16:55:53

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

by lxzxj_chan @ 2017-04-10 16:07:14

尝试下用double型


by bert @ 2017-04-17 20:44:18

#include <iostream>
using namespace std;
int main(){
    double Sn = 0.00000000000;
    int n = 1, K;
    cin >> K;
    while (Sn <= K){
        Sn += 1.00000000000 / n;
        ++n;
    };
    cout << n-1 << endl;
    return 0;
}
这个是正确的

by 攀岩高手 @ 2017-05-13 11:04:59

用int类型做除法,返回值也是整型,所以会导致死循环.请用double.


by 123456zmy @ 2017-05-29 14:30:33

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

|