为什么这样不能过?

P1035 [NOIP2002 普及组] 级数求和

Jason12 @ 2021-12-28 00:04:57

#include <bits/stdc++.h>
  using namespace std;
int main()
{
    int k,n=1;
    double s=0;
    cin>>k;
    while (s<k || s==k)
    {
        s=s+1/n;
        n++;
    }
    cout<<n<<endl;
    return 0;
} 

by kevin006 @ 2021-12-28 00:07:17

精度问题,请尝试使用整数


by TheSky233 @ 2021-12-28 00:26:42

s=s+1/n;

改为

s=s+1.0/n

by Jason12 @ 2022-01-02 21:00:09

@TheSky233 谢谢大佬!Thanks♪(・ω・)ノ


|