我正在为我的Java解决方案收到UVa Online Judge问题的运行时错误。我已完成Problem 100,它在我的最后工作。任何想法可能导致这个问题?无法重现UVa Online Judge给我的运行时错误
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);
}
}
}
你得到什么运行时错误?您的JRE/JDK版本可能会有所不同吗?你的JVM可以不同吗? – Jeffrey 2012-02-19 23:48:13
@Tyler Crompton:*(不是你的问题的答案,因此评论)* ...没有理由再使用Java * Hashtable *而不是* HashMap *。 * Hashtable *是遗留代码(并且因为很长时间),并且性能会比* HashMap *差。如果您确实需要同步映射,那么* Collections.synchronizedMap(...)*或* ConcurrentHashMap *(添加方法,如* putIfAbsent *)就是您要查找的内容。在你的情况下你的代码是单线程的:你只需要一个常规的HashMap。 – TacticalCoder 2012-02-19 23:49:20
@ user988052,谢谢。修正(不是在帖子中,虽然)。 – 2012-02-19 23:56:28