Fork me on GitHub

Java的快速幂

 1 import java.util.Scanner;
 2 
 3 public class C {
 4     public static long power(int x,int n) {
 5         if(n == 0)
 6             return 1;
 7         long t = power(x,n/2);
 8         t = t*t;
 9         if(n%2 == 1)
10             t = t*x;
11         return t;
12     }
13     public static void main(String[] args) {
14         // TODO Auto-generated method stub
15         Scanner in = new Scanner(System.in);
16         int x,n;
17         x = in.nextInt();
18         n = in.nextInt();
19         long t = power(x,n);
20         System.out.println(t);
21     }
22 
23 }

 

posted @ 2018-03-18 18:22  梦想飞的菜鸟  阅读(862)  评论(0)    收藏  举报