[LeetCode]Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

思考:参考链接:http://www.cnblogs.com/pkuoliver/archive/2010/10/06/sotry-about-sqrt.html

class Solution {
public:
    int sqrt(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        double ans = x;        
        while(abs(ans * ans - x) > 0.0001)
        {
            ans = (ans + x / ans) / 2;
        }        
        return (int)ans;
    }
};

  

原文地址:https://www.cnblogs.com/Rosanna/p/3429669.html