北师大ACM新手指导第十二题-高精度乘法应用
Time Limit: 1000 ms Case Time Limit: 1000 ms Memory Limit: 65536 KB
Submit: 150 Accepted: 54
Description
正式比赛时,高精度乘法的题目一般都有较大的变形,选手应该充分理解高精度的算法,然后对应不同的题目,对代码作相应的修改,以下是一道正式的ACM高精度题,它可以算作一道比较典型的ACM题目:
1、虽然有现成的算法,却无法直接套用,必须根据题目作相应的变型。
2、数据有一定的陷阱,要考虑到各种特殊的情况。
好,让我们一起努力把这道题攻下吧:)
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.
This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
Input
The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.The input is end by EOF.
Output
The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.
Sample Input
95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201
1 //http://acm.bnu.edu.cn/contest/problem_show.php?pid=4122 2 import java.io.*; 3 import java.text.*; 4 import java.math.*; 5 import java.util.*; 6 public class Main { 7 8 /** 9 * @param args 10 */ 11 public static void main(String[] args) { 12 // TODO 自动生成的方法存根 13 Scanner cin = new Scanner(new BufferedInputStream(System.in)); 14 BigDecimal a,ans; 15 double x; 16 int b; 17 DecimalFormat fa = new DecimalFormat("############################################################"); 18 DecimalFormat fb = new DecimalFormat(".########################################################################################################################"); 19 while(cin.hasNext()){ 20 x = cin.nextDouble(); 21 b = cin.nextInt(); 22 if(x == 0){ 23 System.out.println("0"); 24 continue; 25 } 26 a = BigDecimal.valueOf(x); 27 ans = a.pow(b); 28 if((x%1)==0) 29 System.out.println(fa.format(ans)); 30 else 31 System.out.println(fb.format(ans)); 32 } 33 } 34 }