• 大数java(pow)


    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 R n where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
    InputThe 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.
    OutputThe 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
     1 import java.util.*;
     2 import java.io.*;
     3 import java.math.*;
     4 import java.text.*;
     5 
     6 public class Main{
     7     public static void main(String[] args){
     8         BigDecimal x;
     9         String ans;
    10         int n;
    11         Scanner cin = new Scanner(System.in);
    12         while(cin.hasNext()){
    13             x = cin.nextBigDecimal();
    14             n = cin.nextInt();
    15             ans = x.pow(n).stripTrailingZeros().toPlainString();//
    16             if(ans.charAt(0)=='0')
    17                 ans = ans.substring(1);
    18             System.out.println(ans);
    19         }
    20     }
    21 }
    22 
    23 // BigDecimal是处理高精度的浮点数运算的常用的一个类
    24 // 当需要将BigDecimal中保存的浮点数值打印出来,特别是在页面上显示的时候,就有可能遇到预想之外的科学技术法表示的问题。
    25 // 一般直接使用 BigDecimal.toString()方法即可以完成浮点数的打印。
    26 // 如:
    27     // System.out.println( new BigDecimal("10000000000").toString());
    28 // 但是,toString()方法输出的字符串并不能保证不是科学计数法。
    29 // 不过在日常的使用中,用toString()方法输出的就是普通的数字字符串而非科学计数法。
    30 // 直接这么写:
    31     // System.out.println( new BigDecimal("100.000").toString());
    32 // 程序的输出即为:  100.000
    33 // 如果我们希望去除末尾多余的0,那么我们应该这么写:
    34     // System.out.println( new BigDecimal("100.000").stripTrailingZeros().toString());
    35 // 其中,stripTrailingZeros()函数就是用于去除末尾多余的0的,但是此时程序的输出为: 1E+2
    36 // 是科学计数法,可能并不是我们想要的。
    37 // 解决的方法很简单,如果想要避免输出科学计数法的字符串,我们要用toPlainString()函数代替toString()。如:
    38     // System.out.println( new BigDecimal("100.000").stripTrailingZeros().toPlainString());
    39 // 此时程序的输出就为 100
  • 相关阅读:
    layui水平导航条三级
    Android环境搭建及Ionic打包(win7)
    brew update usr/local must be wrtable
    vs未能正确加载CSharpPackage包,未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包
    设计模式之策略模式
    设计模式之组合模式
    线程同步之事件
    .Net使用163smtp发送邮件时错误:邮箱不可用. has no permission解决方法
    线程同步之临界区
    线程同步方式之互斥量Mutex
  • 原文地址:https://www.cnblogs.com/jxust-jiege666/p/6947285.html
Copyright © 2020-2023  润新知