Example 4 taking the square root

#sqrt(3)=1; sqrt(4)=2; sqrt(5)=2; sqrt(10)=3

class Solution:
    def sqrt(self, x):
        l, r  = 0, x
        while l + 1 < r:
            m = (r + l) // 2
            if m * m == x:
                return m
            elif m * m > x:
                r = m
            else:
                l = m
        if l * l == x:
            return l
        if r * r == x:
            return r
        return l

if __name__ == '__main__':
    temp = Solution()
    x1 = 5
    x2 = 10
    print(("输入:" + str(x1)))
    print(("输出:" + str(temp.sqrt(x1))))
    print(("输入:" + str(x2)))
    print(("输出:" + str(temp.sqrt(x2))))

The results are as follows:
Input: 5
Output: 2
Input: 10
Output: 3

Guess you like

Origin www.cnblogs.com/hankleo/p/11809675.html