TTTTLLLLEEEE!!!!

P1035 [NOIP2002 普及组] 级数求和

comcnSBhhh @ 2024-03-23 17:01:51

rt

#include<bits/stdc++.h>
using  namespace  std;
int  main()
{
  double k,n;
  scanf("%lf",&k);
  n=0;
  for(int i=1;;i++)
  {
      n+=1/i;
      if(n>k)
      {
          break;
      }
  }
  printf("%lf",n);
  return 0;
}

by _QWQ_TAT_ @ 2024-03-23 17:06:34

@comcnSBhhh 你这里的1/i没有强制转换成浮点形,导致你的代码会在这个for循环中不断重复,就T了


by comcnSBhhh @ 2024-03-23 17:09:14

@scx123456 还是TLE /(ㄒoㄒ)/~~


by _QWQ_TAT_ @ 2024-03-23 17:18:24

我看了,按照从前往后算的话,你先算1/i,再算*0.1,还是T


by _QWQ_TAT_ @ 2024-03-23 17:20:02

所以你得把*0.1往前放


by _QWQ_TAT_ @ 2024-03-23 17:25:37

@comcnSBhhh 忘记@了


by comcnSBhhh @ 2024-03-23 17:29:09

@scx123456 谢谢dalao指导


|