我正在爲我的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