sqrtx-开平方

Implementint sqrt(int x).

Compute and return the square root of x.

逐次逼近

class Solution {
public:
    int sqrt(int x) {
        if(x<2)
            return x;
        int left=1,right=x/2;
        int mid;
        while(left<=right)
        {
            mid=left+(right-left)/2;
            if(x/mid <mid)
                right=mid-1;
            else if(x/mid>mid)
                left=mid+1;
            else
                return mid;
        }
        return right;
    }
};
原文地址:https://www.cnblogs.com/zl1991/p/9630755.html