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

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

how to determine the size of the array in java?
Послано bentham 12 май 2011 09:12
Hello

I have seen all the topics here, but I still dont know how do you calculate the size of the array, also I have written code but I dont know how to get the output once you write the input in the program, I am a beginner, I program in java, here is my code, all kind of help really appreciated

 BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
 String in;
 StringTokenizer str;
 double[] nums = new double[500000];???
 int index = 0;
while ((in = input.readLine()) != null) {
            str = new StringTokenizer(in);
            while (str.hasMoreTokens()) {
                nums[index] = Double.parseDouble(str.nextToken());
                index++;
            }
        }

Edited by author 12.05.2011 09:13

Edited by author 12.05.2011 09:13

Edited by author 12.05.2011 09:13
Re: how to determine the size of the array in java?
Послано AterLux 12 май 2011 12:56
1) input not more than 256KB = 262144 Bytes, consequently there no more than 131072 numbers in input. That's size of array you need

2) Instead of creating instance of StringTokenizer for each line in input, you can simply using StreamTokenizer (like in FAQ: http://acm.timus.ru/help.aspx?topic=java ) reading nval when nextToken() == StreamTokenizer.TT_NUMBER until it not equals to TT_EOF

3) For formatted output you can use:
System.out.printLn(String.format("%.4f", numberVariable));
because String.format implicitly creating new Formatter object, you can speed up program and reduce memory usage by creating one instance:
Formatter f = new Formatter();
...
System.out.printLn(f.format("%.4f", numberVariable));

be sure you're using Locale.US as default, in other way there comma instead of period can appear in place of decimal-separator.
You can manually set Locale.setDefault(Locale.US); in beggining of your program

4) anyway to reach greatest speed and least memory usage for input-output, you can write your own method to read/write numbers from/into input/output streams using once-createrd byte arrays.
Re: how to determine the size of the array in java?
Послано bentham 13 май 2011 03:19
Thanks for answering, here its my code, I dont really understand all your idea about StreamTokenizer, can you check my code please

import java.io.*;

public class Prueba {

    public static void main(String args[]) throws IOException {


        StreamTokenizer str = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        double[] l = new double[131072];

        int index = 0;


        while (((str.nextToken() == StreamTokenizer.TT_NUMBER && str.nextToken() != StreamTokenizer.TT_EOF))) {
            l[index] = str.nval;

        }

        double res;
        for (int i = index - 1; i >= 0; i--) {
            res = Math.sqrt(l[i]);
            System.out.println(String.format("%.4f\n", res));
        }

    }
}
Re: how to determine the size of the array in java?
Послано AterLux 13 май 2011 12:53
You are twice exctracting nextToken, so you can lose some values.
and don't forget to increase index ;)

     int tok;
     while ((tok = str.nextToken()) != StreamTokenizer.TT_EOF) {
       if (tok == StreamTokenizer.TT_NUMBER) // you can skip this line because all input guarantee to contain only numbers
         l[index++] = str.nval;
     }

Re: how to determine the size of the array in java?
Послано AterLux 13 май 2011 12:55
Re: how to determine the size of the array in java?
Послано bentham 13 май 2011 22:17
I already solve the first one, but I got problems in this exercise, my loop never ends, so my solve method does not work, I already submit this code, and I got AC, but I still dont understand what end of file means in this context??, really thanks for all
this is my code,

import java.io.*;

public class Exercise {

    static int index = 0;
    static double[] l = new double[131072];

    public static void main(String args[]) throws IOException {
        StreamTokenizer str = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        while (((str.nextToken() != StreamTokenizer.TT_EOF))) {

            if (str.ttype == StreamTokenizer.TT_NUMBER) {
                l[index++] = str.nval;
                //System.out.println(str.nval);
               // System.out.println(l[0]);
               // System.out.println(l[1]);
            }


        }
        solve();

    }

    public static void solve() {

        double res;
        for (int i = index - 1; i >= 0; i--) {
            res = Math.sqrt(l[i]);
            System.out.println(String.format("%.4f\n", res));
        }
    }
}

Edited by author 13.05.2011 22:47
Re: how to determine the size of the array in java?
Послано Harchenko Vladimir 8 июл 2011 15:10
while (str.nval != StreamTokenizer.TT_EOF)  put to stream -1(TT_EOF) for terminate.
Re: how to determine the size of the array in java?
Послано Harchenko Vladimir 14 июл 2011 01:18
import java.util.Scanner;
import java.util.Stack;

public class SQRT {

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

    private void run()
    {
        Scanner in = new Scanner(System.in);
        Stack<Long> st = new Stack<Long>();


        while (in.hasNextLong())
         {
                  st.push((long) in.nextLong());
         }

        while (st.isEmpty() != true)
        {
             double result = Math.sqrt(st.pop());
            System.out.printf("%.4f\n",result);
        }

    }
}