poj 1001 Exponentiation

Exponentiationjava

Time Limit: 500MS   Memory Limit: 10000K
Total Submissions: 173641   Accepted: 41958

Descriptionui

    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.code

Inputip

    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.ci

Outputinput

    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.string

Sample Inputit

95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12

Sample Outputio

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

题目大意:求m^n,保证精度以及考虑小数的特殊输出。table

注意: 题目不难。但考虑的状况不能漏。请确保以下输入和输出

95123.0 12
548815620517731830194541899025343415715973535967221869852721
95123 12
548815620517731830194541899025343415715973535967221869852721
95.123 12
548815620517731830194541.899025343415715973535967221869852721
0.95123 12
.548815620517731830194541899025343415715973535967221869852721
0.951230 12
.548815620517731830194541899025343415715973535967221869852721
951230 12
548815620517731830194541899025343415715973535967221869852721000000000000

Solution: 代码比较长,基本是别人的5倍。。m^n主要是利用BigDecimal当作整数计算,而后再考虑小数点的位置。

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

public class Main {
    public static String dot(String res, String s, int n) { //给得出的临时结果添加小数点和整数
        s = s.substring(s.indexOf(".") + 1);
        char[] cArr = s.toCharArray();
        int end = cArr.length - 1;
        for (int i = cArr.length - 1; i >= 1; i--) { //小数最末尾的0无效,不影响结果精度
            if (cArr[i] == '0') { 
                end--;
            } else {
                break;
            }
        }
        String result = "";
        int count = (end + 1) * n; //小数点后数的个数
        if (res.length() <= count) {
            int n0 = count - res.length();
            while (n0-- > 0) {
                result += "0";
            }
            result = "." + result + res;
        } else {
            result = res.substring(0, res.length() - count) + "." + res.substring(res.length() - count);
        }

        return result;
    }

    public static String calculate(BigDecimal b, int n) { // 先按整数计算得出临时结果
        BigDecimal result = new BigDecimal(1);
        for (int i = 0; i < n; i++) {
            result = result.multiply(b);
        }
        return result.toString();
    }

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = "";
        while ((line = br.readLine()) != null) {
            String[] s = line.split("\\s+");
            int n = Integer.parseInt(s[1]);
            if (s[0].indexOf(".") == -1) { // 针对95123 12这种状况
                System.out.println(calculate(new BigDecimal(Integer.parseInt(s[0])), n));
            } else if(Integer.parseInt(s[0].substring(s[0].indexOf(".") + 1)) == 0) { // 针对95123.0 12
                System.out.println(calculate(new BigDecimal(Integer.parseInt(s[0].substring(0, s[0].indexOf(".")))), n));
            } else {
                // 用于去掉相似0.951230末尾的0这种状况
                String t = s[0].replace(".", "");
                int end = t.length();
                for (int i = end - 1; i >= 0; i--) {
                    if (t.charAt(i) == '0') {
                        end--;
                    } else {
                        break;
                    }
                }
                t = t.substring(0, end);
                System.out.println(dot(calculate(new BigDecimal(Integer.parseInt(t)), n), s[0], n));
            }
        }
        br.close();
    }
}

    最终结果:

固然,也能够不用BigDecimal, 利用原始加减乘除的计算方式来手动计算临时的整数结果

相关文章
相关标签/搜索