题目:实现函数Power,求base的exponent次方。不得使用库函数,同时不需要考虑大数问题。
def power(b,n): if n==0: return 1 if n<1: return 1/power(b,-1*n) if n%2: return b*power(b,n-1) return power(b*b,n//2)
注:
如果n为偶数,则只需要计算b的n/2次方即可,然后平方。如果n为奇数,转为b乘以n的偶数次方,然后利用递归。终止条件为n为0时,返回1.