最裸的高精度幂,记录一下Java里面高精度类的用法而已
1 import java.io.*; 2 import java.util.Scanner; 3 import java.math.*; 4 5 public class Main 6 { 7 public static void main(String[] args) 8 { 9 Scanner cin = new Scanner(System.in); 10 while(cin.hasNext()) { 11 int m; 12 String sn; 13 sn = cin.next(); 14 m = cin.nextInt(); 15 BigDecimal n = new BigDecimal(sn); 16 String ans = n.pow(m).toPlainString(); 17 if(ans.charAt(0) != '0') System.out.print(ans.charAt(0)); 18 int len = ans.length(); 19 while(ans.charAt(len - 1) == '0') len--; 20 if(ans.charAt(len - 1) == '.') len--; 21 for(int i = 1;i < len;i++) System.out.print(ans.charAt(i)); 22 if(len == 1 && ans.charAt(0) == '0') System.out.print('0'); 23 System.out.print(' '); 24 } 25 } 26 }
其中BigDecimal类的toPlainString方法可以将浮点数转化成精确形式的字符串而不是科学计数法表示