Page 1 of 1

Java 1.6.0 supported? YES!!!

Posted: Sat Sep 15, 2007 2:49 am
by annhy
Congratulations!! :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol:

Java guys, Java 1.6.0 is supported on the new judge system.
We can really enjoy it. :D

Here is a sample code for p100.
I wonder it can be used to replace the old Java sample code on the website
http://acm.uva.es/p/data/p100.java.html

Special Note:
1. Your class name should be Main
2. Your class should be located in default package

Code: Select all

import java.io.*;
import java.util.Scanner;

/**
 * Problem 100: The 3n + 1 problem
 */
public class Main {

  public static int cycleLength(long n) {
    int count = 1;
    while (n > 1) {
      count++;
      n = ((n & 1) == 0) ? (n >> 1) : (3 * n + 1);
    }
    return count;
  }

  public static void main(String[] args) throws IOException {
    Scanner cin = new Scanner(System.in);
    PrintStream cout = System.out;

    while (cin.hasNextInt()) {
      int a = cin.nextInt();
      int b = cin.nextInt();

      int maxCycleLen = 0;
      int sIdx = Math.min(a, b);
      int eIdx = Math.max(a, b);
      for (int i = sIdx; i <= eIdx; i++) {
        maxCycleLen = Math.max(maxCycleLen, cycleLength(i));
      }

      cout.printf("%d %d %d\n", a, b, maxCycleLen);
    }
  }
}

Posted: Sun Sep 16, 2007 10:30 pm
by Darko
Just a note - I would advise against using Scanner like that. If you are using Scanner, at least wrap the System.in in a BufferedReader, something like this:

Code: Select all

Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
Why? I had a TLE on various contests because of Scanner. Those solutions passed when I replaced Scanner with BufferedReader (and did my own parsing). I would suggest omitting Scanner altogether unless you know for sure that the input file is not large.

Another thing - if you are using Scanner and you mix nextInt() and nextLine() you might get unexpected results (like when using scanf() and gets() together).

Posted: Wed Nov 07, 2007 8:27 am
by annhy
Darko wrote:Just a note - I would advise against using Scanner like that. If you are using Scanner, at least wrap the System.in in a BufferedReader, something like this:

Code: Select all

Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
Thanks, Darko. Your advise is quite reasonable.
But strangely, I tried BufferedReader / BufferedWriter in the new server, but the improvement is unobvious.
I have no idea how this could happens.
I tried many different buffer sizes, and got the same results.

Just a note, too :D - I would advise to use StringBuffer to accumulate the massive output. And a single System.out.print() before exiting the program.

I found that it can be about 3 times faster than using lots of System.out.print() calls.

Posted: Wed Nov 07, 2007 8:41 am
by Darko
There is StringBuilder, which is unsynchronized (read: faster) version of StringBuffer.

You can use write() instead of print() and println() - it is much faster, just make sure you flush the buffer once you are done.

I actually put everything in a byte array and write() everything at once (there is write(byte[],int,int)). In my experience, that is the fastest way to do it. Well, at least when the I/O speed is important (which, IMO, should never be the case, but it happens).

Don't get me wrong, I use Scanner on small input files, because it is the most convenient way to do it. Of course, when it times out, I suspect I/O first.

Posted: Sun Nov 18, 2007 2:39 pm
by Eiger Yap
Thanks 4 instruction. I am Newbie here. I wanna ask how about use file in Java?? So what different between 1.5.0 n 1.6.0 ???

Posted: Tue Nov 20, 2007 9:33 pm
by sclo
Eiger Yap wrote:Thanks 4 instruction. I am Newbie here. I wanna ask how about use file in Java?? So what different between 1.5.0 n 1.6.0 ???
There's no need to use files on UVa. All problems takes input from stdin and output to stdout.

Posted: Sat Nov 24, 2007 4:59 pm
by Eiger Yap
can u give example???Image

Re: Java 1.6.0 supported? YES!!!

Posted: Sun Dec 06, 2009 10:30 pm
by sukljan
There is written on the page that the sun JDK 1.6 is being used. If I use String.split instead of StringTokenizer (which is deprecated) I always get a runtime error. Any ideas?