zoukankan      html  css  js  c++  java
  • 大数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
  • 相关阅读:
    Python 如何计算当前时间减少或增加一个月
    删除 win8.1中的网络1,网络2,宽带连接1,宽带连接2等网络记录
    Office2003/2010等集成SP的简单方法
    win8.1点击“更改电脑设置”无反应(闪退)
    右键菜单添加带图标的Notepad++
    word2010无法打开文件时的一点对策
    在win7/8/10鼠标右键添加“管理员取得所有权”
    VisualSVNServer 无法启动 could not log pid to file
    半年来经销商云平台工作总结-后端
    半年来经销商云平台工作总结-前端
  • 原文地址:https://www.cnblogs.com/jxust-jiege666/p/6947285.html
Copyright © 2011-2022 走看看