hogwarts
(霍格沃兹测试学院官方)
2022 年5 月 25 日 02:02
1
实现函数 solution(base:float, x:int),求base的x次方。
注意:
1.base和x不同时为0。
【示例】
输入:base=2.00000, x=3
输出:8.00000
解释:因为2.00000的三次方运算后是8.00000
题目难度:中等
题目来源:牛客网-整数次方
def solution(base:float, x:int)-> float:
# your code here
assert solution(2.00000,3) == 8.00000
assert solution(2.10000,3) == 9.26100
joyoko
(徐子宇)
2022 年5 月 25 日 02:49
2
def solution(base: float, x: int) -> float:
# your code here
if base == 0 and x == 0:
return float(-1)
else:
return round(base ** x, 5)
assert solution(2.00000, 3) == 8.00000
assert solution(2.00000, -2) == 0.25000
assert solution(2.10000, 3) == 9.26100
assert solution(0, 0) == -1.0
def solution(base:float, x:int)-> float:
if x <0:
base=1/base
x=-x
sum=1
for i in range(x):
sum=sum*base
return round(sum,5)
assert solution(2.00000,3) == 8.00000
assert solution(2.10000,3) == 9.26100
def solution(base:float, x:int)-> float:
# your code here 这个题有点一言难尽了啊
return round(pow(base,x),5)
assert solution(2.00000,3) == 8.00000
assert solution(2.10000,3) == 9.26100
def solution(base:float, x:int)-> float:
if base == 0 and x <= 0:
return -1
else:
return ("%.5f" %(base ** x))
def solution(base:float, x:int)-> float:
multiplication=1
count = 0
while count< abs(x):
multiplication = multiplication*base
count += 1
if x>=0:
return '%.5f' %multiplication
else:
return '%.5f' %(1/multiplication)
def solution(base: float, x: int) -> float:
# your code here
# return round(base ** x, 5)
return round(pow(base, x), 5)
assert solution(2.00000, 3) == 8.00000
assert solution(2.10000, 3) == 9.26100
def solution(base:float, x:int)-> float:
return round((base**x),5)
assert solution(2.00000,3) == 8.00000
assert solution(2.10000,3) == 9.26100