数值的整数次方
数值的整数次方_牛客网
https://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00?tpId=13&tqId=11165&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
思路:需要考虑指数为负,底数为0,指数为负时底数为0,指数为0等情况。
AC代码:
def Power(self, base, exponent): # write code here if base == 0: if exponent < 0: return False else: return 0 if exponent == 0: return 1 n = abs(exponent) mul = 1 while n: mul *= base n -= 1 if exponent < 0: mul = 1 /mul return mul