5.x的n次幂(python)
问题描述: 实现函数Pow(x, n),计算并返回x的n次幂。 问题示例: Pow(2, 3) = 8 代码实现:
class Solution:
def myPow(self
, x
, n
):
if n
< 0:
x
= 1 // x
n
= -n
ans
= 1
t
= x
while n
!= 0:
if n
% 2 == 1:
ans
*= t
t
*= t
n
//= 2
return ans
if __name__
== "__main__":
solution
= Solution
()
x
, n
= map(int, input("请输入整数x和n:").split
(" "))
print("结果为:", solution
.myPow
(x
, n
))
转载请注明原文地址:https://blackberry.8miu.com/read-14102.html