ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1001. Reverse Root

my java answer, had passed, but too slow and waste memory!
Posted by gjlsx 28 Mar 2009 12:19
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.Writer;
import java.util.Stack;
import java.util.LinkedList;

public class ACM1001 {

    Reader reader = null;
    Writer writer = null;
    StreamTokenizer in = null;
    PrintWriter out = null;
    boolean oj = System.getProperty("ONLINE_JUDGE") != null;
    Stack<Double> dInput = null;
    LinkedList<Double> dOuput = null;
    BufferedReader bufReader= null;

     public void run()
       {
           try{
               reader = oj ? new InputStreamReader(System.in) : new FileReader("input1001.txt");
               bufReader= new BufferedReader(reader);
               in = new StreamTokenizer(bufReader);
               writer = new OutputStreamWriter(System.out);
               out = new PrintWriter(writer);

               dInput = new Stack<Double>();
               dOuput = new LinkedList<Double>();
               int j = in.nextToken();
               while(j != StreamTokenizer.TT_EOF)
                {
                   if(j == StreamTokenizer.TT_NUMBER)
                   {
                       dInput.push(in.nval);
                   }
                   j = in.nextToken();
                }
           solve();
           out.flush();
           }
           catch (Exception e)
            {
                 e.printStackTrace();
            }
       }

    public void solve()
    {
        //long 类型的最大值的常量,该值为 2^63 -1 至  2^-63
        //double:(2-2-52)·2^1023 至 2^-1074
        while(!dInput.isEmpty()){
            double dva = dInput.pop();
            double x=Math.sqrt(dva);
            dOuput.add(x);
        }
        //BigDecimal 未不可变的、任意精度的有符号十进制数
        while(!dOuput.isEmpty()){
            System.out.printf("%.4f \n",dOuput.removeFirst());
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        new ACM1001().run();
    }

}
Re: just modify, memory used lowed half;
Posted by gjlsx 28 Mar 2009 12:51
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.Writer;
import java.util.Stack;
import java.util.LinkedList;

public class ACM1001 {

    Reader reader = null;
    Writer writer = null;
    StreamTokenizer in = null;
    PrintWriter out = null;
    boolean oj = System.getProperty("ONLINE_JUDGE") != null;
    Stack<Double> dInput = null;
    LinkedList<Double> dOuput = null;
    BufferedReader bufReader= null;

     public void run()
       {
           try{
               reader = oj ? new InputStreamReader(System.in) : new FileReader("input1001.txt");
               bufReader= new BufferedReader(reader);
               in = new StreamTokenizer(bufReader);
               writer = new OutputStreamWriter(System.out);
               out = new PrintWriter(writer);

               dInput = new Stack<Double>();
               dOuput = new LinkedList<Double>();
               int j = in.nextToken();
               while(j != StreamTokenizer.TT_EOF)
                {
                   if(j == StreamTokenizer.TT_NUMBER)
                   {
                       dInput.push(in.nval);
                   }
                   j = in.nextToken();
                }
           solve();
           out.flush();
           }
           catch (Exception e)
            {
                 e.printStackTrace();
            }
       }

     public void solve()
    {
        //long 类型的最大值的常量,该值为 2^63 -1 至  2^-63
        //double:(2-2-52)·2^1023 至 2^-1074
        while(!dInput.isEmpty()){
            double dva = dInput.pop();
            double x=Math.sqrt(dva);
            dOuput.add(x);
        }
        //BigDecimal 未不可变的、任意精度的有符号十进制数
        while(!dOuput.isEmpty()){
            System.out.printf("%.4f \n",dOuput.removeFirst());
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        new ACM1001Solve2().run();
    }

}

class ACM1001Solve2 extends ACM1001
{
     public void run()
       {
           try{
               reader = oj ? new InputStreamReader(System.in) : new FileReader("input1001.txt");
               bufReader= new BufferedReader(reader);
               in = new StreamTokenizer(bufReader);
               writer = new OutputStreamWriter(System.out);
               out = new PrintWriter(writer);

               dInput = new Stack<Double>();
               int j = in.nextToken();
               while(j != StreamTokenizer.TT_EOF)
                {
                   if(j == StreamTokenizer.TT_NUMBER)
                   {
                       solve2(in.nval);
                   }
                   j = in.nextToken();
                }
               bufReader.close();
               bufReader = null;
               reader.close();
               reader = null;
               while(!dInput.isEmpty()){
                    System.out.printf("%.4f \n",dInput.pop());
                }
           out.flush();
           dInput.clear();
           dInput = null;
           out = null;
           writer.close();
           writer = null;
           }
           catch (Exception e)
            {
                 e.printStackTrace();
            }
           finally{
               reader = null;
               writer = null;
             }
       }

     private void solve2(double nn)
     {
        double x=Math.sqrt(nn);
        dInput.push(x);
     }
}//end class ACM1001Solve2