ソードフィンガーオファーインタビューの質問16.数値整数パワー[中]-高速パワー

インタビューの質問16.値の整数乗

class Solution {
public:
    double myPow(double x, int n) {
        if(n==0)    return 1;
        long N=n;
        if(n<0){
            x=1/x;
            N=-N;
        }
        double res=1;
        while(N){
            if(N&1) res*=x;
            x*=x;
            N>>=1;
        }
        return res;
    }
};

 

おすすめ

転載: blog.csdn.net/qq_41041762/article/details/105891009