Find the power of n

In fact, the BigInteger method has pow which is to find the power

package _6数学问题;

public class m快速幂的运算 {
    
    
public static void main(String[] args) {
    
    
	int cifang=2;
	int number=3;//3*3*3*3
	long a=ex(number,cifang);
	System.out.println(a);
}
private static int ex(int number, int cifang) {
    
    
	// TODO Auto-generated method stub
	if(cifang==0) {
    
    
		return 1;
	}
	if(cifang==1) {
    
    
		return number;
	}
	int temp=number;//的一次方
	int res=1;
	int exponent=1;//指数乘方
	while((exponent<<1)<number) {
    
    
		temp=temp*temp;
		exponent=exponent<<1;//指数翻一倍
	}
	res *=ex(number,cifang-exponent);
	return res*temp;
}
}

рекомендация

отblog.csdn.net/weixin_45952706/article/details/109123276