spicy_strip @ 2022-12-28 11:29:04
#include<stdio.h>
int main()
{
int k,n=0;
double sn=0;
scanf("%d",&k);
while(1)
{
if(sn>k)
break;
++n;
sn+=1.0/n;
}
printf("%d",n);
return 0;
}
by ZQIN_P @ 2022-12-28 11:32:02
while(1)
{
if(sn>k)
break;
++n;
sn+=1.0/n;
}
改成
while(1)
{
sn+=1.0/n;
if(sn>k) break;
++n;
}
by ZQIN_P @ 2022-12-28 11:32:41
把n
初值赋为1
by ZQIN_P @ 2022-12-28 11:32:51
@spicy_strip
by spicy_strip @ 2022-12-28 14:20:04
@planet_over_for_ever 交上了 你真厉害 可是为什莫我的那个不对啊
by ZQIN_P @ 2022-12-28 14:29:05
@spicy_strip 您那个加完以后没有及时比较大小,会导致输出永远大一
by small_Dongpo @ 2023-02-05 15:33:41
其实不需要while (1)
循环,可以简化成以下代码:
while (sum <= k)
{
sum += 1.0 / ++i;
}