求A^B的最后三位

已知0<A,B<10000, 求A^B的最后三位。

import java.math.BigDecimal; import java.util.Arrays; /** * @author clydelou * */ public class LastThree { /** * @param args */ public static void f(int a, int n) { int[] r = new int[3]; int x = 0, y = 0; r[0] = a % 10; r[1] = a % 100 / 10; r[2] = a % 1000 / 100; int temp; for (int i = 1; i < n; i++) { x = y = 0; temp = r[0] * a; r[0] = temp % 10; if (temp >= 10) { x = temp / 10; } temp = x; if (r[1] != 0) { temp += r[1] * a; } r[1] = temp % 10; if (temp >= 10) { y = temp / 10; } temp = y; if (r[2] != 0) { temp += r[2] * a; } r[2] = temp % 10; } System.out.println(Arrays.toString(r)); BigDecimal big_a = new BigDecimal(Integer.toString(a)); System.out.println(big_a.pow(n)); } public static void main(String[] args) { // TODO Auto-generated method stub f(123456789, 401); } }

你可能感兴趣的:(求A^B的最后三位)