哪里出错了呀?

P1035 [NOIP2002 普及组] 级数求和

AIMILIIIII @ 2022-01-13 18:45:17


int main()
{
  int k,n;
  double s;
  scanf("%d",&k);
  if(k<1||k>15)  printf("error");
  else
  {
    while(s<=k)
    {
      n=1;
      s=s+1/n;
      n=n+1;
    }
    printf("%d",n);
  }
  return 0;
}```

by xhz_ @ 2022-01-13 18:54:18

可能是s没有初始化?


by Missa @ 2022-01-13 19:33:50

while(s<=k)
{
  n=1;
  s=s+1/n;
  n=n+1;
}

n 的赋值放到循环外面


by Mugino_Shizuri @ 2022-01-14 14:14:05

s没有赋初值


|