HDU Exponentiation 1063 Java大数题解

Exponentiation

Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6973    Accepted Submission(s): 1975

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

大数的题目使用Java就变得其简单无比。

主要就一句代码:

String ans = a.pow(b).stripTrailingZeros().toPlainString();

去零和转换为纯字符串输出。

这使用C++要数十行代码。

对照C++解法:http://blog.csdn.net/kenden23/article/details/23997827

相比之下。java就成了水题了。学不到什么东西。

import java.math.BigDecimal;
import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext())
        {
            BigDecimal a = scan.nextBigDecimal();
            int b = scan.nextInt();
            String ans = a.pow(b).stripTrailingZeros().toPlainString();
            if (ans.startsWith("0")) ans = ans.substring(1);
            System.out.println(ans);
        }
        scan.close();
    }
}
时间: 2024-11-04 17:41:35

HDU Exponentiation 1063 Java大数题解的相关文章

hdu 1042 N! java大数及判断文件末尾

N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 73503    Accepted Submission(s): 21308 Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in on

poj1001 Exponentiation【java大数】

Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 183034   Accepted: 44062 Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the n

多校第六场 HDU 4927 JAVA大数类

题目大意:给定一个长度为n的序列a,每次生成一个新的序列,长度为n-1,新序列b中bi=ai+1?ai,直到序列长度为1.输出最后的数. 思路:这题实在是太晕了,比赛的时候搞了四个小时,从T到WA,唉--对算组合还是不太了解啊,现在对组合算比较什么了-- import java.io.*; import java.math.*; import java.util.*; public class Main { public static void main(String[] args) { Sca

HDU 4927 Series 1 java大数

java mle前会wa 或者 t 这种事我会乱说? import java.math.*; import java.util.*; import java.io.*; public class Main { BigInteger[] a = new BigInteger[3007]; public void work() { int T; T = cin.nextInt(); while (T-- > 0) { int n; n = cin.nextInt(); for (int i = 0;

HDU 1250 Hat&#39;s Fibonacci(Java大数相加)+讲解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n -

hdu 2054 A == B ? (java大数)

题目意思: http://acm.hdu.edu.cn/showproblem.php?pid=2054 给出两个数,判断是否相等,相等输出YES,否则输出NO. 题目分析: 注意题目给的是两个数,没有说明格式和大小,只能字符串模拟比较,或者java大数比较.这里给出java的大数比较. AC代码: import java.math.BigDecimal; import java.math.BigInteger; import java.util.Scanner; public class Ma

HDU 4919 打表找规律 java大数 map 递归

== oeis: 点击打开链接 瞎了,x.mod(BigInteger.ValueOf(2)).equal( BigInteger.ValueOf(1)) 写成了 x.mod(BigInteger.ValueOf(2)).equal( 1 ) T^T100块没了... import java.math.*; import java.util.*; import static java.lang.System.out; import java.io.*; public class Main { s

hdu 4927 Series 1(组合,java大数)

http://acm.hdu.edu.cn/showproblem.php?pid=4927 比赛时分分钟推出来公式后,陷入无限的tle中.起初想处理一遍,但是3000的数量内存装不下,直接编译就不过.为了不超内存,然后又试着把三个数合并为一个,四个数合并一个,100个数合并为一个,还是TLE.. 说到底还是数学不好啊,杨辉三角的第n行的第m个数为组合数c[n-1][m-1]. 应用c[n][m] = c[n][m-1]*(n-m+1)/m,就可以快速递推出第n行的数,这样就能省去预处理的时间以

HDU 5047 Sawtooth (JAVA大数类)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047 题面: Sawtooth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1636    Accepted Submission(s): 637 Problem Description Think about a plane: ● O