现在的位置: 首页 > 综合 > 正文

PKU 1001 Exponentiation

2012年08月05日 ⁄ 综合 ⁄ 共 1537字 ⁄ 字号 评论关闭

PKU 1001 Exponentiation

Run ID User Problem Result Memory Time Language Code Length Submit Time
5068074 kingpro 1001 Accepted 1108K 94MS Java 1469B 2009-04-29 19:18:29
PKU 1001 Exponentiation

 1 import java.io.BufferedReader;
 2 import java.io.IOException;
 3 import java.io.InputStreamReader;
 4 import java.math.BigDecimal;
 5 import java.util.logging.Level;
 6 import java.util.logging.Logger;
 7 
 8 public class Main {
 9 
10     public static void main(String[] args) {
11         try {
12             BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
13             String data = null;
14             while ((data = reader.readLine()) != null) {
15                 String[] d = data.split("\\s+");
16                 BigDecimal r = new BigDecimal(d[0]);
17                 int n = Integer.parseInt(d[1]);
18                 BigDecimal result = r.pow(n);
19                 String res = result.toPlainString();
20                 int i = 0;
21                 for (; i < res.length(); i++)
22                     if (res.charAt(i) != '0')
23                         break;
24                 int j=res.length()-1;
25                 for(; j>=i; j--)
26                     if(res.charAt(j)!='0')
27                         break;
28                 if(res.charAt(j)=='.')
29                     j--;
30                 System.out.println(res.substring(i, j+1));
31             }
32         } catch (IOException ex) {
33             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
34         }
35     }
36 }

 

Java BigDecimal类 使用toPlainString()方法获取全部位

抱歉!评论已关闭.