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

bugs found in the code

Posted by frkstyc at 2006-05-24 11:55:12
In 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:
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