问题
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);
}
}
}
回答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.
回答2:
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.
来源:https://stackoverflow.com/questions/9354482/cant-reproduce-a-runtime-error-that-uva-online-judge-gives-me