1. 程式人生 > >Exponentiation(高精度大數)

Exponentiation(高精度大數)

nsis tint 大數 any rail while har create 高精度

Exponentiation

Description

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.

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

Hint

If you don‘t know how to determine wheather encounted the end of input:
s is a string and n is an integer

//計算 n 的 x 次方

//顯然,是個大數題,正好來練練java,格式要控制好。

技術分享
 1 import java.math.BigDecimal;
 2 import java.util.Scanner;
 3 import java.lang.String;
 4 
 5 /**
 6  * Created by happy_code on 2017/6/6.
 7  */
 8 public class Main {
 9     public  static void  main(String[] args){
10         Scanner sc = new Scanner(System.in);
11         String s;
12         int n;
13         while (sc.hasNext())
14         {
15             s=sc.next();
16             n=sc.nextInt();
17             BigDecimal base = new BigDecimal(s);
18             BigDecimal res = new BigDecimal("1");
19             for (int i=0;i<n;i++){
20                 res = res.multiply(base);
21             }
22             String ans = res.toPlainString();
23 
24             boolean ok = false;
25             int k;
26             for (k =0;k<ans.length();k++){
27                 if (ans.charAt(k)==.){
28                     ok = true;
29                     break;
30                 }
31             }
32             int e=ans.length()-1;
33             if (ok){
34                 while(ans.charAt(e)==0) e--;
35                 if (ans.charAt(e)==.) e--;
36             }
37             int i = 0;
38             while(ans.charAt(i)==0) i++;
39             for (;i<=e;i++){
40                 if (i==0&&ans.charAt(i)==0){
41                     continue;
42                 }
43                 System.out.print(ans.charAt(i));
44             }
45             System.out.println();
46         }
47     }
48 }
View Code

Exponentiation(高精度大數)