"""
@author: xbw
@user: Administrator
@time: 2021/4/15 0015/23:11
@name: 指数算法
@tool: PyCharm
"""
from math import floor
class Solution(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
if x == 1 or n == 0:
return 1
n2 = n
if n < 0:
x = 1 / x
n2 = -n
return mulitDeep(x, n2)
def mulitDeep(x, n):
if n == 1:
return x
if n % 2 != 0:
resuslt = mulitDeep(x, floor(n / 2))
return x * resuslt * resuslt
else:
resuslt = mulitDeep(x, floor(n / 2))
return resuslt * resuslt
if __name__ == '__main__':
result = Solution().myPow(2, -3)
print(result)