Java 1.6.0 supported? YES!!!

Write here if you have problems with your Java source code

Moderator: Board moderators

Post Reply
annhy
New poster
Posts: 40
Joined: Sun May 27, 2007 1:42 am
Location: Taiwan

Java 1.6.0 supported? YES!!!

Post 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);
    }
  }
}
Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post 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).
annhy
New poster
Posts: 40
Joined: Sun May 27, 2007 1:42 am
Location: Taiwan

Post 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.
Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post 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.
Eiger Yap
New poster
Posts: 4
Joined: Sun Nov 18, 2007 2:30 pm
Location: Medan-Indonesia
Contact:

Post 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 ???
sclo
Guru
Posts: 519
Joined: Mon Jan 23, 2006 10:45 pm
Location: Vancouver, BC, Canada
Contact:

Post 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.
Eiger Yap
New poster
Posts: 4
Joined: Sun Nov 18, 2007 2:30 pm
Location: Medan-Indonesia
Contact:

Post by Eiger Yap »

can u give example???Image
sukljan
New poster
Posts: 1
Joined: Sun Dec 06, 2009 10:21 pm

Re: Java 1.6.0 supported? YES!!!

Post 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?
Post Reply

Return to “Java”