Exponentiation(高精度大数)

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

//计算 n 的 x 次方

//显然,是个大数题,正好来练练java,格式要控制好。

 1 import java.math.BigDecimal;
 2 import java.util.Scanner;
 3 import java.lang.String;
 4
 5 /**
 6  * Created by happy_code on 2017/6/6.
 7  */
 8 public class Main {
 9     public  static void  main(String[] args){
10         Scanner sc = new Scanner(System.in);
11         String s;
12         int n;
13         while (sc.hasNext())
14         {
15             s=sc.next();
16             n=sc.nextInt();
17             BigDecimal base = new BigDecimal(s);
18             BigDecimal res = new BigDecimal("1");
19             for (int i=0;i<n;i++){
20                 res = res.multiply(base);
21             }
22             String ans = res.toPlainString();
23
24             boolean ok = false;
25             int k;
26             for (k =0;k<ans.length();k++){
27                 if (ans.charAt(k)==‘.‘){
28                     ok = true;
29                     break;
30                 }
31             }
32             int e=ans.length()-1;
33             if (ok){
34                 while(ans.charAt(e)==‘0‘) e--;
35                 if (ans.charAt(e)==‘.‘) e--;
36             }
37             int i = 0;
38             while(ans.charAt(i)==‘0‘) i++;
39             for (;i<=e;i++){
40                 if (i==0&&ans.charAt(i)==‘0‘){
41                     continue;
42                 }
43                 System.out.print(ans.charAt(i));
44             }
45             System.out.println();
46         }
47     }
48 }

时间: 2024-10-08 22:37:03

Exponentiation(高精度大数)的相关文章

hdu 1063 Exponentiation (高精度小数乘法)

//大数继续,额,要吐了. 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 re

POJ 1001 Exponentiation 无限大数的指数乘法 题解

POJ做的非常好,本题就是要求一个无限位大的指数乘法结果. 要求基础:无限大数位相乘 额外要求:处理特殊情况的能力 -- 关键是考这个能力了. 所以本题的用例特别重要,再聪明的人也会疏忽某些用例的. 本题对程序健壮性的考查到达了变态级别了. 某人贴出的測试用例数据地址: http://poj.org/showmessage?message_id=76017 有了这些用例,几下调试就过了. 我关键漏了的用例: 000.10  1 000000  1 000.00  1 .00000  0 0000

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

Hdu 4762 网络赛 高精度大数模板+概率

注意题目中的这句话he put the strawberries on the cake randomly one by one,第一次选择草莓其实有N个可能,以某一个草莓为开头,然后顺序的随机摆放,所以最后的概率为n/m^(n-1),最后通过大数模板搞定该题的化简. C++代码 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<iomanip> 5 #include

uva 10106 Product(高精度大数乘法)

昨天刚写了个大数加法,今天又来了个大数乘法,其实解法差不多,只不过换成了好多个大数的相加而 已,看别人的算法其实跟我的也差不多,都是这个姿势.wa了一次,竟然忘了考虑0的情况,以后交题之前,都要判 断一下边缘数据,大数据和小数据,要不就是白白被扣时间啊 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm> using namespace std; char a[

PAT A1024题解——高精度大数相加模板

#include<stdio.h>#include<string.h>#include<algorithm>typedef long long ll;using namespace std;struct bign{    int d[1000];    int len;    bign(){        memset(d,0,sizeof(d));        len = 0;    }}; bign change(char str[]){     //将整数转换为

高精度大数c++类模板 很好用

首先声明这是大佬写的,我只是记录下,拿来学习.附上大佬的链接 :https://blog.csdn.net/code4101/article/details/23020525 代码: #include <stdio.h> #include <stdlib.h> #include <iostream> #include <string> #include <cstring> #include <cstdio> using namespa

POJ-1001-Exponentiation(高精度大数)

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

HDU-1002(简单大数加法)

A + B Problem II Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases.