自用留档
public class Fast_Power {
public static void main(String[] args)
{
long a = 2,b = 8;
fastPower(a,b);
System.out.println(fastPower(a,b));
}
public static long fastPower(long a, long b){
long ans = 1;
while(b != 0) {
if(b % 2 == 1) {
ans = ans * a;
}
a = a*a;
b /= 2;
}
return ans;
}
}
该代码实现了一个名为Fast_Power的程序,主要功能是进行快速幂运算。在main方法中,以2为底数,8为指数进行计算。fastPower函数使用了循环优化的幂运算,通过不断将指数b右移并判断余数来减少计算次数,提高效率。
320

被折叠的 条评论
为什么被折叠?



