Poj1001 求高精度冪
阿新 • • 發佈:2018-12-23
package com.coderli.acm.poj;
import java.math.BigDecimal;
import java.util.Scanner;
/**
* Time Limit: 500MS Memory Limit: 10000K Description
*
* 對數值很大、精度很高的數進行高精度計算是一類十分常見的問題。比如,對國債進行計算就是屬於這類問題。
*
* 現在要你解決的問題是:對一個實數R( 0.0 < R < 99.999 ),要求寫程式精確計算 R 的 n 次方(Rn),其中n 是整數並且 0 < n
* <= 25。
*
* Input T輸入包括多組 R 和 n。 R 的值佔第 1 到第 6 列,n 的值佔第 8 和第 9 列。
*
* Output 對於每組輸入,要求輸出一行,該行包含精確的 R 的 n 次方。輸出需要去掉前導的 0 後不要的 0 。如果輸出是整數,不要輸出小數點。
*
* Sample Input
*
* 95.123 12<br>
* 0.4321 20<br>
* 5.1234 15<br>
* 6.7592 9<br>
* 98.999 10<br>
* 1.0100 12<br>
*
* Sample Output
*
* 548815620517731830194541.899025343415715973535967221869852721
* .00000005148554641076956121994511276767154838481760200726351203835429763013462401
* 43992025569.928573701266488041146654993318703707511666295476720493953024
* 29448126.764121021618164430206909037173276672
* 90429072743629540498.107596019456651774561044010001
* 1.126825030131969720661201
*
* @author OneCoder
* @blog http://www.coderli.com
* @date 2013年10月14日 下午1:21:26
*/
public class Poj1001 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
BigDecimal bdR = input.nextBigDecimal();
int n = input.nextInt();
for (int i = 0; i < n; i++) {
}
BigDecimal result = bdR.pow(n);
// 去掉結尾無用的0
String strResult = result.stripTrailingZeros().toPlainString();
// 去掉開頭的無用的0
if (strResult.startsWith("0.")) {
strResult = strResult.substring(1);
}
System.out.println(strResult);
}
}
}