这个为什么过不了?

P1035 [NOIP2002 普及组] 级数求和

yangdoubao @ 2021-10-26 23:06:45

#include<iostream>

#include<cstdio>

using namespace std;

int main()

{

    double s=0.0000;

    int n,k;

    cin>>k;

    for(n=1;s<=k;n++)  s+=(1.0/n);

    printf("%d",n);

    return 0;

}

by Mr_ll @ 2021-10-27 09:55:55

@yangdoubao 输出n-1; for循环,如果是s<=k成立,执行循环体,然后n++;也就是说,你的s已经大于k了,但n++了;


by Wangjiankai @ 2021-12-06 22:20:49

@yangdoubao 输出n-1; for循环,如果是s<=k成立,执行循环体,然后n++;也就是说,你的s已经大于k了,但n++了;


|