POJ 1001 Exponentiation

这道题是计算实数的N次方问题,对这样要求高精度的地方,double是肯定不够用的(double的精度只有16位)。看到题的第1感觉是可能需要用数组来计算,但越想越复杂,找找看有无比较简单的解决方法,发现BigDecimal可以用来处理有效位超过16位的数。BigDecimal不能使用简单的+-*/,说明BigDecimal类其实在数的基础上进行了封装。

初版代码写得很烂,而且提交1直有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();
}
}
}

第2天晚上继续修改,还是没通过,参考他人是怎样写的,发现了1个非常简洁高效的代码。

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);
}
}
}

对照两段代码,我的代码有3处可以优化(基本上全部进行了优化)。

1、数据输入

当初使用BufferedReader是为了1次读1行,但是没必要,Scanner的功能更贴切,由于Scanner有nextBigDecimal()和nextInt()函数,后续还不需要转换数据格式。

2、幂的计算

BigDecimal有个pow()函数可以直接使用,完全没有必要使用for循环来计算。(这1步优化更多的是性能的提高,不是引发runtime error的地方)

3、结果打印

result.intValue()和result.doubleValue()与1比较,其实都不准确。还是直接比较字符串更好,而且代码更加简洁。

附、float和double的精度问题可以参考这个

http://www.cnblogs.com/fromchaos/archive/2010/12/07/1898698.html

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » POJ 1001 Exponentiation

1 评论

  1. I really love to read such an excellent article. Helpful article. Hello Administ . Website Giriş için Tıklayin. casibom

评论已关闭

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡