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

I really don't know why it is wrong(Java Scrip)
Posted by Hufeng Zhou 17 Aug 2010 20:04
Hi, I tried to use java to solve this problem but failed, yet do not know where is the bug,
I have pasted the source code here, wish someone can help me out. I am very very grateful.

public class ReversRoot1001 {
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new FileReader("ReversRoot1001.txt"));

        Vector<Integer> record = new Vector<Integer>();

        String line;
        while((line = br.readLine())!=null){
            Scanner sc = new Scanner(line);
            int a = sc.nextInt();
            record.add(a);
//            if(sc.hasNext()){
//                int b = sc.nextInt();
//                //int c = sc.nextInt();
//                record.add(b);
//            }
            while(sc.hasNext()){
                int b = sc.nextInt();
                record.add(b);
            }
        }

        for(int i = record.size();i<record.size();i--){
            System.out.println(record.get(i));
        }
        br.close();
    }

}
Re: I really don't know why it is wrong(Java Scrip)
Posted by Moonstone 17 Aug 2010 20:56
Use standart input/output. And don't use Scanner - it's very slow.

Look:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer tok = null;

String readToken() throws IOException { // reads the token; to read a full line use in.readLine()
    while (tok == null || !tok.hasMoreTokens()) {
        tok = new StringTokenizer(in.readLine());
    }
    return tok.nextToken(); // sometimes it's better to use nextToken(String) method here
}

int readInt() throws IOException { // write readDouble(), readLong(), readBigInteger() methods if necessary
    return Integer.parseInt(readToken());
}

And, finally, don't forget to write out.flush() at the end of your program!

Edited by author 17.08.2010 21:26
Re: I really don't know why it is wrong(Java Scrip)
Posted by Hufeng Zhou 18 Aug 2010 06:20
Moonstone Thank you so much for your help.
I am very grateful!

Edited by author 18.08.2010 06:21