不知道算法哪里有问题,一直60,求解

P1035 [NOIP2002 普及组] 级数求和

浪尖取水饮 @ 2018-11-24 18:50:55

include <stdio.h>

int main () { int k,n=1; float s=0.0; scanf ("%d",&k); for (;s<=k;n++) s=s+1.0/n; printf ("%d",n-1); }


by 浪尖取水饮 @ 2018-11-24 18:53:16

啊,抱歉第一次用,发错了


by 浪尖取水饮 @ 2018-11-24 18:53:50

啊,抱歉第一次用,发错了```


int main ()
{ int k,n=1;
 float s=0.0;
  scanf ("%d",&k);
  for (;s<=k;n++)
  s=s+1.0/n;
  printf ("%d",n-1);
}

by Reaepita @ 2018-11-24 18:58:07

@莉亚的C3PO 最后答案不需要-1吧


by 浪尖取水饮 @ 2018-11-24 19:36:26

@Harry_bh 要的啊


by 南极的太阳 @ 2018-11-28 12:19:34

float的有效数字位只有6至7位,当n比较大时,就超出范围了,要用double。


|