//幂运算,
public class POW {
public static long pow(long x, int n){
if(n == 0)
return 1;
if(n == 1)
return x;
if(isEven(n))//是否 偶数,
return pow(x * x, n / 2);
else
return pow(x * x, (n - 1) / 2) * x;
}
public static boolean isEven(int n){
if(n % 2 == 0)
return true;
else return false;
}
public static void main(String[] args){
long x =1000;
int n = 2;
long z = pow(x, n);
System.out.println("x的n次幂:" + z);
}
}
//幂运算,
最新推荐文章于 2021-03-23 18:11:39 发布