Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

新手的问题,答案都对的就是WA,我已经注意到纯小数个位的0省略,小数末尾的0切掉,求解答,谢谢

Posted by yankee_whiskey at 2011-01-29 13:51:41 on Problem 1001
代码,用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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator