60分求助(C语言)

P1035 [NOIP2002 普及组] 级数求和

Sakanako2333 @ 2022-10-26 21:46:15

为什么这个不对捏

#include <stdio.h>

int main(void){
    int n = 0;
    int k;
    float sum = 0;

    scanf("%d", &k);

    do{
        n++;
        sum += (1.0 / n);
    }while(sum <= k);

    printf("%d", n);

    return 0;
}

by zhangzhichu123456 @ 2022-10-26 21:49:14

建议把sum换成double型(来自隔壁c++的建议


by ZXXS @ 2022-10-26 21:50:10

@Sakanako2333 float精度不够,改成double


by Sakanako2333 @ 2022-10-26 21:54:38

@ZXXS 谢谢,过了!


by Sakanako2333 @ 2022-10-26 21:54:58

@zhangzhichu123456 谢谢!过了


|