Checking if a number is perfect square?

后端 未结 8 2021
野趣味
野趣味 2021-02-10 01:10

I think there is a precision problem with the following code:

bool isPerfectSquare(long long n){
    long long squareRootN=(long long)(sqrt(n)+0.5);

    return          


        
8条回答
  •  伪装坚强ぢ
    2021-02-10 01:31

    You could use the result of the floating point square root as a hint only. Convert it to an integer. Check if the square is equal. If it is higher or lower, decrement or increment it, then recheck for the square, and continue until you have bound the parameter with: c1*c1 <= n <= (c1+1)*(c1+1)

提交回复
热议问题