大佬看看什么问题

P1035 [NOIP2002 普及组] 级数求和

fys666 @ 2022-10-06 01:23:46

#include<stdio.h>
int main()
{
    int k,n=1,cnt=0;
    float sum;
    scanf("%d",&k);

    for(sum=0;k>=sum;n++){
        sum+=1.0/n;
        cnt++;
    }
    printf("%d",cnt);
    return 0;
}

by ssytxy2024 @ 2022-10-06 01:38:41

@方亚申 n是整数,除不了


by bamboo12345 @ 2022-10-06 07:00:44

@方亚申 好像double就可过


by hjsxhst2022 @ 2022-10-06 08:12:06

@方亚申 float sum;除非题目有特别要求,否则不要用float,用double,不然很容易精度不够


by fys666 @ 2022-10-06 13:47:52

@hjsxhst2022 好的,谢谢大佬


|