标签:
这道题是计算实数的N次方问题,对于这样要求高精度的地方,double是肯定不够用的(double的精度只有16位)。看到题的第一感觉是可能需要用数组来计算,但越想挺复杂,找找看有没有其他比较简单的解决方法,发现BigDecimal可以用来处理有效位超过16位的数。BigDecimal不能使用简单的+-*/,说明BigDecimal类其实在数的基础上进行了封装。
第一版代码写得很烂,而且提交一直有runtime error。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String text = br.readLine();
while(!text.isEmpty()){
String[] str = text.split(" ");
if(str.length == 2) {
BigDecimal R = new BigDecimal(str[0]);
BigDecimal result = R;
int N = Integer.parseInt(str[1]);
for(int i=1; i<N; i++){
result = result.multiply(R);
}
if(result.intValue() < 1) {
System.out.println(result.toPlainString().substring(1));
} else {
System.out.println(result.stripTrailingZeros());
}
}
text = br.readLine();
}
}
}
import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
BigDecimal R = in.nextBigDecimal();
int n = in.nextInt();
R = R.pow(n);
String str = R.stripTrailingZeros().toPlainString();
if (str.startsWith("0."))
str = str.substring(1);
System.out.println(str);
}
}
}1、数据输入
当初使用BufferedReader是为了一次读一行,但是没必要,Scanner的功能更贴切,因为Scanner有nextBigDecimal()和nextInt()函数,后续还不需要转换数据格式。
2、幂的计算
BigDecimal有个pow()函数可以直接使用,完全没有必要使用for循环来计算。(这一步优化更多的是性能的提高,不是引起runtime error的地方)
3、结果打印
result.intValue()和result.doubleValue()与1比较,其实都不准确。还是直接比较字符串更好,而且代码更加简洁。
附、float和double的精度问题可以参考这个
http://www.cnblogs.com/fromchaos/archive/2010/12/07/1898698.html
标签:
原文地址:http://blog.csdn.net/luopeng123456789/article/details/42755917