首页 > 专题系列 > Java解POJ > POJ 1001 Exponentiation [解题报告] Java
2013
11-08

POJ 1001 Exponentiation [解题报告] Java

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

输入:

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.

输出:

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.

样例输入:

95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12

样例输出:

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

温馨提示:

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 */
{
...
}

解题代码:

方法(一):
//* @author: [email protected]
import java.util.Scanner;
import java.math.*;

public class Main{

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			BigDecimal bd = new BigDecimal(sc.next());
			BigDecimal result = bd.pow(sc.nextInt());
			String s = result.stripTrailingZeros().toPlainString();
			if(s.startsWith("0"))
				s=s.substring(1);
			System.out.println(s);
		}
	}
}

方法(二):
import java.util.*;   
import java.io.*;   
import java.math.*;   
  
public class Main {   
    public static void main(String[] args) {   
        Scanner scan = new Scanner(new BufferedReader(   
                new InputStreamReader(System.in)));   
           
        while(scan.hasNext())   
        {   
            BigDecimal bd = scan.nextBigDecimal();   
            int p = scan.nextInt();   
               
            String str = bd.pow(p).toPlainString();   
               
            int endFlag = str.length();   
                           
            for(int i = endFlag-1; i >= 0; i--)   
            {   
                if(str.contains(".") && str.charAt(i) == '0')   
                    endFlag = i;   
                else  
                    break;   
            }   
               
            str = str.substring(0, endFlag);   
            if(str.startsWith("0."))   
                str = str.substring(1);   
               
            if(str.endsWith("."))   
                str = str.substring(0,str.length()-1);   
            System.out.println(str);   
        }   
    }   
}

  1. 博主您好,这是一个内容十分优秀的博客,而且界面也非常漂亮。但是为什么博客的响应速度这么慢,虽然博客的主机在国外,但是我开启VPN还是经常响应很久,再者打开某些页面经常会出现数据库连接出错的提示

  2. 有一点问题。。后面动态规划的程序中
    int dp[n+1][W+1];
    会报错 提示表达式必须含有常量值。该怎么修改呢。。

  3. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮

  4. Pingback: bottes vernies blanches