Fast power operation (java)

Fast power operation: exponential multiple growth is used to speed up power operation, and the remaining power can be solved by recursion.
code show as below:

import java.util.Scanner;
public class Ksms {
//快速幂运算
 static int mis(int a,int b)
 {
  int c=a;
  if(b==1)//递归出口
   return a;
  int temp=1;
  int res=1;
  while((temp<<1)<b)//利用指数倍数增长来加快速度
  {
      c*=c;
   temp=temp<<1;
  }
  res*=mis(a,b-temp);//计算剩余的指数次幂
  return c*res;
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub
      Scanner a=new Scanner(System.in);//输入底数和幂次
      int b=mis(a.nextInt(),a.nextInt());
      System.out.println(b);
 }
}

The operation has been added as follows:

Insert picture description here
Insert picture description here

Published 14 original articles · praised 0 · visits 240

Guess you like

Origin blog.csdn.net/lianggege88/article/details/105440121