全WA求助

P1035 [NOIP2002 普及组] 级数求和

229074269zhouxuanyi @ 2023-05-28 15:47:22

#include <stdio.h>
int main() {
    int k,i;
    double sum = 0.0;
    scanf("%d",&k);
    for ( i = 1; i <= k; i++)
    {
        sum+=1.0/i;
        if (sum>k){
            break;
        }

    }
    printf("%d",i);
    return 0;
}

不知道为什么全WA了 求助大佬


by lcy5419 @ 2023-05-28 15:52:20

@229074269zhouxuanyi

    for ( i = 1; i <= k; i++)

改为

    for ( i = 1;; i++)

by 229074269zhouxuanyi @ 2023-05-28 15:53:29

@lmx100127 理解了 谢谢!


by Tim_the_best @ 2023-06-06 20:34:32

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

|