为什么自己的编译器上能过,洛谷不能过呀,大佬们

P1035 [NOIP2002 普及组] 级数求和

sanfenzhupi @ 2023-12-04 16:59:08

#include<iostream>
using namespace std;
int main()
{
    int k = 0;
    cin >> k;
    double sum = 0;
    for (double i = 1;; i++)
    {

        if (sum <k)
        {
            sum += 1 / i;
        }
        else
        {
            cout << i;
            break;
        }
    }

    system("pause");
    return 0;
}

by penguin_is_cool @ 2023-12-04 17:06:27

@sanfenzhupi

可能是iostream在洛谷上不包含system("");试试用万能库:<bits/stdc++.h>

补充:

memset只用iostream在洛谷上也不行。


by penguin_is_cool @ 2023-12-04 17:07:40

@sanfenzhupi 不是你第8行那个for循环写的是个啥?


by sanfenzhupi @ 2023-12-04 17:22:48

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

    system("pause");
    return 0;
}

@penguin_is_cool 又修改了一下,佬,您再看看这个咋样


by penguin_is_cool @ 2023-12-04 17:54:45

@sanfenzhupi emm,你那个for循环改成for (double i=0;i<1e9;i++)或者用while循环。

参考:

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    double n,i=1.0;
    cin>>n;
    double sum=0;
    sum+=1.0/i;
    while (sum<=n){
        i++;
        sum+=1/i;
    }
    cout<<i;
}

by sanfenzhupi @ 2023-12-04 18:43:48

@penguin_is_cool 好的,谢谢您


by chen__pengyu @ 2023-12-17 15:48:06

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

给你


|