为什么?

P1035 [NOIP2002 普及组] 级数求和

SZH0603 @ 2023-10-19 22:23:56

#include<stdio.h>
int main()
{
    double k;
    int i = 1;
    double sum = 0;
    scanf("%lf",&k);
    while(sum<k)
    {
        sum = sum + 1*1.0/i;
        i++;
    }
    printf("%d",i-1);
    return 0;
} 

上面这个代码过不了样例,但是可以AC是什么情况?


by bycwxbd @ 2023-10-19 23:22:07

可能,也许,1是个例外?


by penguin_is_cool @ 2023-10-24 17:06:53

sum=1/i就行了


by Angus66 @ 2023-10-29 11:00:21


#include <bits/stdc++.h>

using namespace std;

int main(){

    int a=0;

    double num=0;

    int i=0;

    cin>>a;

    while(num<a){

        i++;

        num=num+1.0/i;

    }

    cout<<i;

    return 0;

}

|