Fork me on GitHub

快速幂运算

public class Solution {
    public static void main(String[] args) {
        System.out.println(qpow(5,6));
    }

    public static int qpow(int x,int n) {
        int res =1;
        while(n!=0){

            if (1==(1 & n)){
                res*=x;
            }
            n >>= 1;
            x*=x;
        }

        return res;
    }
}

 

posted @ 2020-09-02 10:26  努力不会错哦  阅读(88)  评论(0)    收藏  举报