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
参考代码:
import java.util.*; public class Main { final public static int MAXN = 200; final public static int CN = 6; public static void main(String[] args) { Scanner scan = new Scanner(System.in); while(scan.hasNext()){ String num = scan.next(); int p = scan.nextInt(); //System.out.println(num+" "+n); System.out.println(pow(num,p)); } } private static StringBuffer pow(String num, int p) { StringBuffer bf = new StringBuffer(); int sd[] = new int[2]; convert(num,sd); bf.append(n2pow(sd[0],p,sd[1])); return bf; } private static StringBuffer n2pow(int n, int p,int fp) { //System.out.println("n="+n+" p="+p+" fp="+fp); StringBuffer bf = new StringBuffer(); int AN[] = new int[MAXN]; int TN[] = new int[MAXN*2]; int RN[] = new int[MAXN*2]; int size = 0,i = 0,j = 0; int point = fp * p; while(n > 0){ AN[size ++] = n % 10; n /= 10; } RN[0] = 1; while(p > 0){ for(i = 0;i < size;++ i){ for(j = 0;j < MAXN;++ j){ TN[i + j] += AN[i] * RN[j]; } } for(i = 0;i < MAXN;++ i){ if(TN[i] >= 10){ TN[i + 1] += TN[i] / 10; TN[i] = TN[i] % 10; } RN[i] = TN[i]; TN[i] = 0; } p --; } boolean bl = false; int len = 0; for(i = MAXN - 1;i >= 0;-- i){ if(RN[i] > 0 || bl){ bf.append(RN[i]); bl = true; } } len = bf.length(); //System.out.println("point="+point+" len="+len); if(point >= len){ StringBuffer tbf = new StringBuffer(); tbf.append("."); for(i = len;i < point;++ i){ tbf.append("0"); } tbf.append(bf); bf = tbf; }else if(point != 0){ String s = new String(bf.reverse()); //System.out.println(s); StringBuffer tbf = new StringBuffer(); char sch[] = s.toCharArray(); for(i = sch.length - 1;i >= 0;-- i){ tbf.append(sch[i]); if(i == point){ tbf.append("."); } } bf = tbf; } //System.out.println(); //System.out.println(bf); return bf; } private static void convert(String num, int[] sd) { String temp[] = num.split("\\."); String v = new String(); v = temp[0]; if(temp.length == 2){ boolean bl = false; char sch[] = temp[1].toCharArray(); StringBuffer bf = new StringBuffer(); for(int i = sch.length - 1;i >= 0;-- i){ if(sch[i] != '0' || bl){ bf.append(sch[i]); bl = true; } } v += bf.reverse(); sd[1] = bf.length(); }else{ sd[1] = 0; } sd[0] = Integer.parseInt(v); //System.out.println("temp1="+temp[1]+" v="+v); } }