| ||||||||||
| 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 | |||||||||
bugs found in the codeIn Reply To:Problem with "runtime error" Posted by:darktemplar207 at 2006-05-23 20:55:52 corrections:
use BigDecimal.toPlainString() to avoid handling the exponent field
suppress the leading zero before decimal point
> Here is my code for exponentiation, for some reason the online judge always gives me a runtime error despite the fact that it works fine on every computer I've tried. Am I doing something stupid?
>
>
> import java.lang.Math;
> import java.io.*;
> import java.util.*;
> import java.util.regex.Pattern;
> import java.lang.Object;
> import java.math.*;
> import java.lang.StringBuilder;
> import java.text.*;
>
> public class Main {
> public static void main(String args[]) throws IOException {
> BigDecimal R;
> int n;
> BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
> String y="";
> StringTokenizer st;
> StringBuffer kb=new StringBuffer();
> do {
> y=f.readLine();
> if(y==null) break;
> st=new StringTokenizer(y);
> y=st.nextToken();
> try {
> R=new BigDecimal(y, MathContext.UNLIMITED);
> }
> catch(Exception e) {
> R=new BigDecimal(y, MathContext.DECIMAL128);
> }
> n=Integer.parseInt(st.nextToken());
> R=R.pow(n);
> R=R.stripTrailingZeros();
> kb.append(R.toString());
> if(kb.indexOf("E")!=-1) {
> int qq= kb.indexOf("E");
> int s=Integer.parseInt(kb.substring(qq+1));
> kb.delete(qq, kb.length());
> if(s<0) {
> s=-s;
> int dec=kb.indexOf(".");
> kb.delete(dec, dec+1);
> for(int i=0; i<s-1; i++) kb.insert(0, "0");
> kb.insert(0, "0.");
> }
> else {
> int dec=kb.indexOf(".");
> kb.delete(dec, dec+1);
> kb.insert(dec+s, ".");
> }
> }
> System.out.println(kb);
> kb.setLength(0);
> } while(true);
> System.exit(0);
> }
> }
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator