Exponentiation
Time Limit: 500MS | Memory Limit: 10000K | |
Total Submissions: 158211 | Accepted: 38520 |
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 R n where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
This problem requires that you write a program to compute the exact value of R n 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
题意:大数问题,显然计算机是存不下的,所以引入字符串模拟大数存储,此处用了java语言,直接调用了java中的相关函数
这个题与nyoj 155 高精度幂 是一样一样的。。。
直接上代码
具体代码:
import java.math.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){//迭代器
BigDecimal a = in.nextBigDecimal();//第一次调用next()返回序列的第一个元素
int b = in.nextInt();//接着使用next()获得序列中的下一个元素。
if(b==0)
System.out.println("1");
else{
BigDecimal c = a.pow(b);//计算a的b次幂
//移除所有尾部零,得到科学计数法的表示格式,然后用toPlainString()方法返回对应值的字符串
String str = c.stripTrailingZeros().toPlainString();
if(str.startsWith("0."))//判断str字符串是否是以"0."开头,即去掉前导0
str = str.substring(1);//从索引1开始赋值给str
System.out.println(str);
}
}
in.close();
}
}
扩展
BigDecimal num=new BigDecimal("0.00").stripTrailingZeros();
System.out.println(num);
输出为0.00
原因
stripTrailingZeros();
API:数值上等于移除所有尾部零的 BigDecimal
但是0.00来说就是[000,2],也就是[0,2,],表示最高位往后有两位小数
如果使用了stripTrailingZeros();即除去后面的0, 对于原来的[0,2]去掉0 写成[0,-2]没有任何变化还是等于0.00
如果整数部分有数值的话,就不一样了。
比如2.00,那么相当于[200,2],然后使用stripTrailingZeros(); 后就相当于[200,-2],那么就会去掉后面的两个00。