TLE 要怎么改--

P1035 [NOIP2002 普及组] 级数求和

hana999 @ 2020-02-02 15:37:59

超时了

#include<stdio.h>
main()
{
    int i,k,Sn=1;
    scanf("%d",&k);
    for(i=2;;i++)
    {
        Sn=Sn+1/i;
        if(Sn>k)
        break;
    }
    printf("%d",i);
    return 0;
}

by Falashiro @ 2020-02-02 15:40:47

@sheeing 您应该使用float


by hana999 @ 2020-02-02 15:47:17

@Forever_Pursuit !还是TLE


by zr太弱了 @ 2020-02-02 15:51:09

@sheeing 死循环


by 灼灼夭夭 @ 2020-02-02 16:00:52

@sheeing 把if条件改到循环内也行


by 灼灼夭夭 @ 2020-02-02 16:01:52

if后输出+retrun 0


by 灼灼夭夭 @ 2020-02-02 16:12:43

 for(i=2;Sn>k;i++)
    {
        Sn=Sn+1/i;
    }

by hana999 @ 2020-02-02 16:18:57

@夜雨心生 这样就WA了


by 灼灼夭夭 @ 2020-02-02 16:47:34

@sheeing 用精度,sn为float或double


by 灼灼夭夭 @ 2020-02-02 16:48:50

@sheeing 要细心


by hana999 @ 2020-02-02 17:07:56

ok了 感谢大佬们


| 下一页