PEKING ACM ID1001

1、问题
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. 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

描述:
解决大且高精度的数字间的计算问题是很常见的。例如,国家债务的计算就是对很多计算机系统的一个考验。
这个问题需要你编写一个程序:计算出R^n的精确的值,其中R是0.0-99.99之间的数,n好似0-25之间的整数。

输入:
输入数据包含R和n的一系列数据,第1列到第6列的数据组成了R,第8列到第9列的数据组成了n;

输出:
输出:每一行的结果对应于上面每一行的输入,最前面的0不允许出现在输出结果中(也就是如果是0.1234需要输出为.1234),未断的无关紧要的”0”也不要被打印出来,如果结果是整数,不要打印小数点。

2、解题思路:

  • 通过输入数据,解析出R 和 N
    通过BufferedReader来读取字符串,并根据R和N的排布截取出R和N的字符串:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line=br.readLine();
String R = line.substring(0, 6);
String N = line.substring(7, 9);
  • 输出数据格式定义
    由于输出结果属于高精度数据,若采用Long数据,则会产生溢出错误,所以使用Java中针对高精度数据新定义的BigDecimal类型。
  • 本实例的整体算法思想为
    先记录R的小数点的位置:position,后将R转换为整型进行求幂的运算,得出结果后再添件小数点,此时小数的位数应为:(5-position)*n;用BigDecimal.pow(n).toPlainString()将求幂的结果转换为字符串,然后再将小数点适当的插入到该字符串中。

3、实现代码如下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;

public class ID_1001 {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while((line=br.readLine())!=null)
        {
            /*
             * 取出第一行并分割字符串取出R、N*/
            String R = line.substring(0, 6);
            String N = line.substring(7, 9);

            /*这个就是要输出的字符串*/
            int subnumber = 1;

            /*将字符串N转换为整数n*/
            if(N.contains(" "))
            {
                N = N.substring(1);
            }
            //System.out.println("R ="+R+"N ="+N);
            int n = Integer.parseInt(N);

            /*对R进行处理,记录小数点的位置,并得出r^n有多少位小数*/
            int position;
            position = R.indexOf(".");
            position = (5-position)*n;

            /*向将R转换为整型,去除小数点,然后进行计算r^n*/
            String temp = R.replace(".", "");
            int r = Integer.parseInt(temp);
            //System.out.println("r = "+r+" n = "+n+"  "+position);

            /*计算r^n*/
            multiply(r,n,position);
        }
    }

    private static void multiply(int r, int n, int position) {
        // TODO Auto-generated method stub

        /*
         * 高精度的数字使用 BigDecimal,*/
        BigDecimal temp=new BigDecimal(r);

        String tempstr = temp.pow(n).toPlainString();
        //System.out.println(tempstr);

        /*
         * 将小数点添加到结果字符串中*/

        /*当position大于tempstr的长度,即R属于0.0XXX*/
        if(position>tempstr.length())
        {
            int k = position-tempstr.length();
            for(int i = 0;i<k;i++)
            {
                tempstr="0"+tempstr;
            }
            tempstr="."+tempstr;
        }/*当R属于0.XXXX时*/
        else if(r<10000){
            tempstr="."+tempstr;
        }/**/
        else{
            position = tempstr.length()-position;

            tempstr = tempstr.substring(0, position)+"."+tempstr.substring(position);
        }

        /*去除末尾无关紧要的0,并且如果是整型,则去掉小数点*/
        int length=tempstr.length();
        while(tempstr.charAt(length-1) == '0')
        {
            length--;
            tempstr = tempstr.substring(0, length);
            if(tempstr.charAt(tempstr.length()-1) == '.')
            {
                tempstr = tempstr.substring(0, length-1);
                break;
            }
        }
        /*输出打印*/
        System.out.println(tempstr);

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值