LeetCode~实现pow函数

double myPow(double x, int n) {
        if(x==0) return 1.0;
        if(n==1) return x;
        unsigned int t;
        if(n > 0)
        {
            t = (unsigned int)n;
        }
        else 
        {
            t = (unsigned int)(-n);
        }
        for(double i=1.0; ;x *= x)
        {
            if((t&1) != 0)
            {
                i *= x;
            }
            if((t>>=1) == 0)
                return n>0?i:(1.0)/i;
        }
    } 

实现double pow(double n, int m)函数

首先判断m是正数还是负数

for循环的解释:

猜你喜欢

转载自blog.csdn.net/weixin_40822052/article/details/88647023