Exponentiation(java 大实数)

http://acm.hdu.edu.cn/showproblem.php?pid=1063

Exponentiation

Time Limit: 2000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8076    Accepted Submission(s): 2279

Problem 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

 1 import java.math.BigDecimal;
 2 import java.util.Scanner;
 3
 4
 5 public class Main {
 6     public static void main(String[] args) {
 7     Scanner s = new Scanner(System.in);
 8     while(s.hasNext()){
 9         BigDecimal b = s.nextBigDecimal();
10         BigDecimal ans = BigDecimal.valueOf(1);
11         int n = s.nextInt();
12         while(n-- > 0)
13             ans = ans.multiply(b);
14         String string = ans.stripTrailingZeros().toPlainString().toString();
15         if(string.startsWith("0."))
16             string = string.substring(1);
17         System.out.println(string);
18     }
19         s.close();
20     }
21 }
1、stripTrailingZeros() ,返回类型为BigDecimal的小于此数的但除去尾部的0的数值。
2、toPlainString(),返回BigDecimal类型的String类型字符串。
3、startsWith(),确定此实例的开头是否与指定的字符串匹配。
4、substring(),返回一个新的字符串,它是此字符串的一个子字符串。该子字符串始于指定索引处的字符,一直到此字符串末尾。

大家都知道JAVA的类一般都要带toString这个方法的,BigDecimal则有toString,toPlainString和toEngineeringString三种表示成字符串的方法,


下面是这三种方法各自的特点:


toString: using scientific notation if an exponent is needed;


toEngineeringString:using engineering notation if an exponent is needed.


toPlainString:without an exponent field.


 
时间: 2024-08-02 06:59:11

Exponentiation(java 大实数)的相关文章

HDU ACM 1063 Exponentiation 大实数乘方

分析:大实数乘方计算. #include<iostream> #include<string> using namespace std; struct BigReal //高精度实数 { int len; //长度 int num[10000]; int point; //小数点位置 BigReal() { len=1; point=0; memset(num,0,sizeof(num)); } }; bool Read(BigReal& a) //读入一个大实数 { st

JAVA大数值问题总结

大数值问题: 如果基本的整数(如:int.long)和浮点数的精度不狗满足需求时,那么就可以用java.math包中的两个类BigInteger(任意精度的整数)和BigDecimal(任意精度的浮点数). 这两个类可以实现人一次长度数字的数值 BigInteger a = BigInteger.valueOf(100);//把100转换为大数类型的整数 使用valueOf()方法可以将普通的数值转化为大数 那么接下来,怎么用大数实现数值运算呢? 在JAVA中提供加法方法就是: BigInteg

42步进阶学习—让你成为优秀的Java大数据科学家!

作者 灯塔大数据 本文转自公众号灯塔大数据(DTbigdata),转载需授权 如果你对各种数据类的科学课题感兴趣,你就来对地方了.本文将给大家介绍让你成为优秀数据科学家的42个步骤.深入掌握数据准备,机器学习,SQL数据科学等. 本文将这42步骤分为六个部分, 前三个部分主要讲述从数据准备到初步完成机器学习的学习过程,其中包括对理论知识的掌握和Python库的实现. 第四部分主要是从如何理解的角度讲解深入学习的方法.最后两部分则是关于SQL数据科学和NoSQL数据库. 接下来让我们走进这42步进

揭秘java大数据学习路线图

很多的同学在学习JavaEE的路上都过得的是坎坷,可以说是夜以继日的敲代码在学习,却发现自己是事倍功半,有的时候遇到一个bug真的很难受,无限互联java大数据培训专家为大家整理了一篇很值得大家去借鉴的学习路线图文章,希望大家在学习的路上一能帆风顺! 一.Java的核心 这就是学习Java的基础,掌握程度的深与浅甚至直接影响后面的整个学习进程. Java的核心主要包括了几个部分: 一. java大数据学习路线图 1.初级的有语法基础.面向对象思想. 学习任何一门语言语法都是必须的,因为Java的

java大数字操作:BigInteger,BigDecimal(浮点型)

java大数字操作: BigInteger:大数字整型的 BigDecimal(浮点型):大数字小数的,也适用大的整数 BigInteger: String num1 = "100381828646148164"; String num2 = "10998979766868"; BigInteger big1 = new BigInteger(num1); BigInteger big2 =new BigInteger(num2); System.out.print

hdu1753(模拟大实数相加)

题目信息: 手动求大实数相加和 http://acm.hdu.edu.cn/showproblem.php?pid=1753 AC代码: /** *大实数相加,以小数点为分界,模拟加法运算,最后合并 */ #include<iostream> #include<string> #include<algorithm> using namespace std; string add(string s1,string s2){//字符串模拟大整数加法,模拟结果有前导0 int

[Java]#从头学Java# Java大整数相加

重操旧业,再温Java,写了个大整数相乘先回顾回顾基本知识.算法.效率什么的都没怎么考虑,就纯粹实现功能而已. 先上代码: 1 package com.tacyeh.common; 2 3 public class MyMath { 4 5 public static String BigNumSum(String... n) { 6 int length = n.length; 7 StringBuilder result = new StringBuilder(); 8 //这里判断其实不需

Java大数据人才应用领域广,就业薪酬高

互联网创造了大数据应用的规模化环境,大数据应用成功的案例大都是在互联网上发生的, 互联网业务提供了数据,互联网企业开发了处理软件,互联网企业的创新带来了大数据应用 的活跃,没有互联网便没有今天的大数据产业.没有互联网.云计算.物联网.移动终端与 人工智能组合的环境大数据也没那么重要.大数据的价值并非与生俱来而是应用创新之结果 ,价值是由技术组合创新涌现出来的.离开环境的支持大数据毫无价值,就像离开了身体的 手不再有手的功能一样. 随着2017年大数据各种应用的发展,大数据的价值得以充分的发挥,大

JAVA 大数据内存耗用测试

JAVA 大数据内存耗用测试import java.lang.management.ManagementFactory;import java.lang.management.MemoryMXBean; public class MemoryTest { public static void main(String[] args) throws InterruptedException { int row = 50_000; int column = 20; String[] data = ne