ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1001. Обратный корень

my java answer, had passed, but too slow and waste memory!
Послано gjlsx 28 мар 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;
Послано gjlsx 28 мар 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