0
点赞
收藏
分享

微信扫一扫

Python计算 x 的整数 n 次幂函数

玉新行者 2023-04-25 阅读 112

Python实现 pow(x, n) ,即计算 x 的整数 n 次幂函数(即,xn )。

def myPow(self, x: float, n: int) -> float:
        if x==0:return 0
        res=1
        if n<0:x,n=1/x,-n
        while n:
            if n&1:res*=x
            x*=x
            n>>=1
        return res
举报

相关推荐

0 条评论