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 |
新手的问题,答案都对的就是WA,我已经注意到纯小数个位的0省略,小数末尾的0切掉,求解答,谢谢代码,用Java写的,用了网上的一个方法把小数末尾的0切掉 import java.util.Scanner; import java.math.*; public class Main { public static void main(String args[]) { Scanner scanner=new Scanner(System.in); String printAns; for(int counter=1;counter<=6;counter++) { BigDecimal base=scanner.nextBigDecimal(); int exponent=scanner.nextInt(); BigDecimal ans=base.pow(exponent); printAns=ans.toPlainString(); if(printAns.startsWith("0.")) { printAns=printAns.replaceFirst("0.", "."); } System.out.println(trim(printAns)); } } public static String trim(String str) { if (str.indexOf(".") != -1 && str.charAt(str.length() - 1) == '0') { return trim(str.substring(0, str.length() - 1)); } else { return str.charAt(str.length() - 1) == '.' ? str.substring(0, str.length() - 1) : str; } } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator