当前位置:编程学习 > JAVA >>

ACM模拟题讲解(1)-高精度

Java中提供了byte、short、int和long表示整数,float和double来表示浮点数,每种类型都有一定的表示范围,当超过了这个范围之后就不能处理了。为了提供对非常大的整数和浮点数的处理,Java提供了BigDecimal和BigInteger。下面的代码演示了BigDecimal和BigInteger的基本用法:

              BigDecimal data1 = new BigDecimal("23232123456789.123456789");

              BigDecimal data2 = new BigDecimal("23423423123456789.123456789");

              BigDecimal data3 = data1.add(data2);

              System.out.println(data3.toString());

             

              BigInteger iData1 = new BigInteger("123123123456456789789123456789");

              BigInteger iData2 = new BigInteger("12312312323232456456789789123456789");

              BigInteger iData3 = iData1.add(iData2);

              System.out.println(iData3.toString());

如果不使用这些类库如何实现大整数和大浮点数的计算呢?下面通过ACM模拟题介绍。

1、Exponentiation(不想看英文,可以直接跳到后面的中文部分)
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 Rn 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. Dont 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


翻译:题目的含义是计算R(0.0 < R < 99.999)的n(0 < n <= 25)次方。

题目解析:

这个题目的关键是如何表示大的整数或者浮点数。针对本题目,计算n次方。可以单独处理小数的位置,如果R中的包含x位小数,则进行运算后的结果应该包括x*n位小数。可以采用数组表示R,然后进行运算即可。下面的方法供参考。

       public static String exponentiation(String data,int n){

              // 找小数点

              int index = data.indexOf(".");

              // 去掉小数点

              String temp = data.replace(".","");

              // 用数组表示输入的数据

              byte[] inputData=new byte[temp.length()];

           // 表示计算结果

              byte[] result = new byte[160];

              // 表示进位

              byte pre=0;

              // 初始化

              for(int i=0;i<inputData.length;i++){

                     result[i] = Byte.parseByte(temp.substring(temp.length()-i-1,temp.length()-i));

                     inputData[i] = result[i];

              }

             

              // 计算n-1遍

              for(int i=0;i<n-1;i++){

                     // 表示上一次的计算结果

                     byte[] tempResult = Arrays.copyOf(result,result.length);

                     // 对result初始化

                     Arrays.fill(result,0,result.length,(byte)0);

                     // 用上一次的计算结果与输入的数据相乘

                     for(int k=0;k<inputData.length;k++){

                            for(int j=0;j<150;j++){

                                   // 两位相乘

                                   byte tempMul = (byte)(tempResult[j]*inputData[k]+result[j+k]);

                                   // 处理个位

                                result[j+k]=(byte)(tempMul%10);

                                // 处理进位

        &n

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,