1

I am getting runtime errors for my Java solutions to UVa Online Judge problems. I have finished Problem 100 and it works on my end. Any ideas what could be causing the problem?

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;

class P100 {

    public static void main(String args[]) {
        Hashtable<Integer, Integer> solutions = new Hashtable<Integer, Integer>();
        Scanner input = new Scanner(System.in);

        while (input.hasNextInt()) {
            int lowerBound = input.nextInt();
            int upperBound = input.nextInt();

            int longestCount = 0;

            for (int i = lowerBound; i <= upperBound; i++) {
                int n = i;
                int count = 1;

                ArrayList<Integer> sequence = new ArrayList<Integer>();

                while (n != 1) {
                    if (solutions.containsKey(n)) {
                        count += solutions.get(n) - 1;
                        break;
                    }

                    sequence.add(n);

                    count += 1;
                    if (n % 2 == 0) n /= 2;
                    else n = 3 * n + 1;
                }

                for (int j = 0; j < sequence.size(); j++) {
                    solutions.put(sequence.get(j), count - j);
                }

                if (count > longestCount) longestCount = count;

                solutions.put(i, count);
            }

            System.out.printf("%d %d %d\n", lowerBound, upperBound, longestCount);
        }
    }

}
animuson
  • 53,861
  • 28
  • 137
  • 147
Tyler Crompton
  • 12,284
  • 14
  • 65
  • 94
  • What runtime errors are you getting? Could your JRE/JDK versions be different? Could your JVMs be different? – Jeffrey Feb 19 '12 at 23:48
  • 2
    @Tyler Crompton: *(not an answer to your question, hence the comment)*... There's no reason anymore to use Java *Hashtable* instead of a *HashMap*. *Hashtable* is legacy code (and since a very long time) and will perform worse than *HashMap*. In case you do need synchronized maps, then *Collections.synchronizedMap(...)* or *ConcurrentHashMap* (adding methods like *putIfAbsent*) are what you're looking for. In your case your code is single-threaded: you just want a regular HashMap. – TacticalCoder Feb 19 '12 at 23:49
  • @user988052, Thanks. Fixed (not in the post, though). – Tyler Crompton Feb 19 '12 at 23:56
  • I'm not sure why this is labeled as off-topic. I included as much information as I could. I had no idea why the code was working nor would UVa give me the error message. – Tyler Crompton Aug 01 '14 at 23:02

2 Answers2

1

You need to rename

class P100

to

public class Main

when you copy the code into UVa or it will tell you that the class Main was not found. This is so the judge can run your code (because java needs to know the class name). I myself forget to do this sometimes.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
  • Ah, this fixed it. Thanks a bunch. Now to figure out where the wrong answer is coming from. *sigh* – Tyler Crompton Feb 19 '12 at 23:55
  • @TylerCrompton This problem is easy enough to brute force. Don't bother being smart because you might just end up shooting yourself in the foot ;) – Jesus Ramos Feb 19 '12 at 23:59
  • It times out after three seconds and my implementation already takes 0.83 seconds (with the unknown input). I can't imagine brute-forcing it. – Tyler Crompton Feb 20 '12 at 00:05
  • @TylerCrompton I wrote both a smart/brute force implementation in C++ and Java and none of them time out. I gave this as an easy one to my students and some received time outs which is why I did it. I see no reason why it would time out even on some pretty big input. – Jesus Ramos Feb 20 '12 at 00:30
0

I had problems running my Java code with the UVa Online Judge. It might not be directly related to your current problem, but anyway take a look at my answer and see if it is of any help.

Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386