Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:谁来帮我看看是哪儿错了?~JAVA的~In Reply To:谁来帮我看看是哪儿错了?~JAVA的~ Posted by:xjtudamajia at 2009-11-23 21:35:52 > 一开始用数组做,一直是wrong answer~ > 怒了,用bigdecimal做~WA~ > 为啥啊,第一题就让咱这么受挫~ package t; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigDecimal; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in)); String line = null; while ((line = stdin.readLine()) != null && line.trim().length() > 0) { try { StringTokenizer st = new StringTokenizer(line); String d = st.nextToken(); int p = Integer.parseInt(st.nextToken()); BigDecimal bdc = new BigDecimal(d); System.out.println(trim(bdc.pow(p).toPlainString())); } catch (Exception e) { // TODO: handle exception // System.out.println(); break; } } } private static String trim(String str) { StringBuffer sb = new StringBuffer(str); while (sb.length() > 0 && sb.charAt(0) == '0') { sb.deleteCharAt(0); } if (str.contains(".") && str.endsWith("0")) { while (sb.length() > 0 && sb.charAt(sb.length() - 1) == '0') { sb.deleteCharAt(sb.length() - 1); } } if (sb.length() == 0) return "0"; else { String rt = sb.toString(); if (rt.equals(".")) { return "0"; } else if (rt.endsWith(".")) { rt = rt.substring(0, rt.length() - 1); } return rt; } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator