设计求a的n次方的算法
package demo;
public class P30 {
//设计求a的n次方的算法
//用a*a、a平方*a平方、a的四次方*a的四次方的速度逼近;剩下的次数用递归计算
public static void main(String[] args) {
System.out.println(pow(2,15));
}
static int pow(int a, int n) {
if(n==0)
return 1;
int result=a;
int times=1;
while((times<<1)<=n) {
result=result*result;
times=times<<1;
}
return result*pow(a, n-times);
}
}

浙公网安备 33010602011771号