题目描述
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
保证base和exponent不同时为0
解答
# coding:utf-8
class Solution:
def Power(self, base, exponent):
# write code here
temp = base # 0的0次方和0的负数次方无意义
if base == 0.0 and exponent <= 0:
return None
# 0的次方为0
if base == 0.0:
return 0
# 非0的次方为1
if exponent == 0:
return 1
# 当exponent大于0
if exponent > 0:
for i in range(exponent - 1):
temp *= base
return temp
# 当exponent小于0
if exponent < 0:
for i in range(-exponent - 1):
temp *= base
return 1.0/temp
结束!