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

Hint

If you don‘t know how to determine wheather encounted the end of input: 
s is a string and n is an integer

C++
while(cin>>s>>n)
{
...
}
c
while(scanf("%s%d",s,&n)==2) //to  see if the scanf read in as many items as you want
/*while(scanf(%s%d",s,&n)!=EOF) //this also work    */
{
...
}

Source

East Central North America 1988

题意:

给定一个浮点数R和一个整数n,求R^n

思路:

想给学弟们出一道大数巩固一下。搜到的大数例题。

java BigDecimal自己也都还没用过。

两个点,用 String s = r.pow(n).stripTrailingZeros().toPlainString();使结果不用科学计数法。

注意去掉前导零。比如0.00001要变成 .00001

 1 import java.io.EOFException;
 2 import java.math.BigDecimal;
 3 import java.math.BigInteger;
 4 import java.util.Scanner;
 5
 6 public class Main {
 7
 8
 9     static public void main(String[] args){
10         Scanner scan = new Scanner(System.in);
11         BigDecimal r;
12         int n;
13         while(scan.hasNext()){
14             r = scan.nextBigDecimal();
15             n = scan.nextInt();
16             String s = r.pow(n).stripTrailingZeros().toPlainString();
17             if(s.startsWith("0")){
18                 s = s.substring(1);
19             }
20             System.out.println(s);
21         }
22         scan.close();
23     }
24 }

原文地址:https://www.cnblogs.com/wyboooo/p/9898388.html

时间: 2024-10-29 02:04:54

poj1001 Exponentiation【java大数】的相关文章

Exponentiation java大数

Exponentiation 大数a的n次幂,直到读到EOF(文件结尾)为止,其中忽略小数后面的0 1 import java.util.*; 2 import java.math.*; 3 import java.text.*; 4 public class Main 5 { 6 public static void main(String[] args) 7 { 8 Scanner cin=new Scanner(System.in); 9 BigDecimal a; 10 int n; 1

poj1001 Exponentiation

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

nyoj 73 比大小 【java大数】

java大数. 代码: import java.util.Scanner; import java.math.*; public class Main{ public static void main(String[] args){ Scanner cin = new Scanner(System.in); BigInteger a, b; BigInteger t = BigInteger.valueOf(0); a = cin.nextBigInteger(); b = cin.nextBi

UVA10303 - How Many Trees?(java大数+catalan数)

UVA10303 - How Many Trees?(java大数+catalan数) 题目链接 题目大意:给你1-N N个数,然后要求由这些数构成二叉搜索树,问有多少种这样的二叉搜索树. 解题思路:把前5项理出来,正好是1 2 5 14 42..就猜想是catalan数,结果也是对了.公式f(i + 1) = (4?i - 6)/ i; (i >= 2).结果很大,要用高精度. 代码: import java.util.*; import java.math.*; import java.io

UVA10183 - How Many Fibs?(java大数+二分)

UVA10183 - How Many Fibs?(java大数+二分) 题目链接 题目大意:给你a,b,求[a,b]之间有多少个fibs数. 解题思路:虽然a.b很大,但是在10^100内的fibs却不超过500个.这样就可以先把这些fibs保存在数组中,然后每次二分去找a,b的位置,然后就可以得到之间有多少个fibs. 代码: import java.util.*; import java.math.*; import java.io.*; import java.lang.String.*

hdu4927 Series 1(组合+公式 Java大数高精度运算)

题目链接: Series 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 423    Accepted Submission(s): 146 Problem Description Let A be an integral series {A1, A2, . . . , An}. The zero-order series o

多校第六场 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

java大数的基本函数

1.读入 Scanner cin=new Scanner(System.in);// 读入 while(cin.hasNextInt()) //等同于!=EOF,第一数一定要输入整形的 { } 大数的一般是: while(cin.hasNextBigInteger())  //第一个数一定要输入大数的 { } while(t-->0)   //等同于while(t--) { } 2.赋值 BigInteger b=BigInteger.valueOf(a); //a可为int,long,stri

ACM-ICPC java(大数)使用总结

今天碰到一道大数除法和模运算的题,以前也写过加减乘的大数模拟运算,但总觉着太麻烦了,今天大体了解了一下Java的输入输出,特来总结一下如何使用java中的高精度类型.首先我们要会建一个简单的java程序(以A+B为例)如下 import java.io.*; import java.util.*; import java.math.*; import java.text.*; public class Main { public static void main(String args[]) {

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;