80分,求调!

P1035 [NOIP2002 普及组] 级数求和

benxin200563 @ 2024-10-28 20:26:06

int main()
{
    int k;
    double sum = 0.0;
    long long cout = 0;
    scanf("%d", &k);
    for (double i = 1; sum < k; i++)
    {
        sum += 1.0/i;
        cout++;
    }
    printf("%lld", cout);

    return 0;
}

by SunXiaolang @ 2024-10-28 20:34:31

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int k,i=1;
    double s=0.0;
    cin>>k;
    while (k>=s){
        s+=1.0/i;
        i++;
    }
    cout<<i-1;
}

@benxin200563 求关


by lty2023 @ 2024-10-28 20:36:28

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

@benxin200563 仅供参考


by SunXiaolang @ 2024-10-28 20:37:09

@benxin200563 建议你用while循环,这会比for循环好一点


by SunXiaolang @ 2024-10-28 20:37:48

@benxin200563 还有,用ld来输出就好了。


by chenxitao @ 2024-10-28 20:39:31

@SunXiaolang 怎么发代码


by benxin200563 @ 2024-10-28 21:17:48

感谢各位的解答!!!


by SunXiaolang @ 2024-10-29 16:36:03

@chenxitao 有一个插入代码,然后选c类型的代码就好了


by chenxitao @ 2024-10-29 18:08:17

@SunXiaolang 谢谢


|