Java大数 poj 1001

本文介绍了一种使用Java的BigDecimal类来实现高精度数值幂运算的方法。通过对实数R的n次方(R^n)进行精确计算,解决了数值计算中常见的精度问题。

求高精度幂
Time Limit: 500MS Memory Limit: 10000K
Total Submissions: 168611 Accepted: 40827

Description

对数值很大、精度很高的数进行高精度计算是一类十分常见的问题。比如,对国债进行计算就是属于这类问题。 

现在要你解决的问题是:对一个实数R( 0.0 < R < 99.999 ),要求写程序精确计算 R 的 n 次方(R n),其中n 是整数并且 0 < n <= 25。

Input

T输入包括多组 R 和 n。 R 的值占第 1 到第 6 列,n 的值占第 8 和第 9 列。

Output

对于每组输入,要求输出一行,该行包含精确的 R 的 n 次方。输出需要去掉前导的 0 后不要的 0 。如果输出是整数,不要输出小数点。

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]  view plain  copy
  1. import java.util.*;  
  2. import java.math.*;  
  3. import java.io.*;  
  4.   
  5. public class poj1001 {  
  6.     public static void main(String args[])  
  7.     {  
  8.         Scanner cin = new Scanner(System.in);  
  9.         while(cin.hasNext())  
  10.         {  
  11.             String a = cin.next();  
  12.             int t = cin.nextInt();  
  13.             BigDecimal ans = new BigDecimal(a);  
  14.             ans = ans.pow(t);  
  15.             String result = ans.stripTrailingZeros().toPlainString();  
  16.             if(result.charAt(0)=='0') result=result.substring(1);  
  17.             System.out.println(result);  
  18.         }  
  19.     }  
  20. }  


math包里有两个处理大数的类:BigInteger 和 BigDecimal,顾名思义一个是用来处理整数的一个是用来处理有理数数的;

add(),subtract(),pow(),abs()之类的常用运算方法都有,直接拿来用就行了。

BigDecimal关于格式控制的方法多了几个,这对处理各种不同格式的输出是很有用的。

stripTraillingZeros():把不影响数值大小的0全去掉;

1.50 ->1.5;

1.00->1;

  if(result.charAt(0)=='0') result=result.substring(1); 如果String当中第一个是0则从下标为1的位置截取。

即 0.0001->.0001;

大家都知道JAVA的类一般都要带toString这个方法的,BigDecimal则有toString,toPlainString和toEngineeringString三种表示成字符串的方法,

下面是这三种方法各自的特点:

toString: using scientific notation if an exponent is needed;//如果需要指数,就用科学计数法

toEngineeringString:using engineering notation if an exponent is needed.//如果需要指数,就用工程符号

toPlainString:without an exponent field.//没有指数




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. 输入说明 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. 输出说明 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. 输入样例 95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 输出样例 548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201 小提示 If you don't know how to determine wheather encounted the end of input: s is a string and n is an integer C++ while(cin>>s>>n) { ... } c while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want /*while(scanf(%s%d",s,&n)!=EOF) //this also work */ { ... } 来源 East Central North America 1988 北大OJ平台(代理
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值