Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
我的UVa Online Judge问题的Java解决方案出现运行时错误。我已经完成Problem 100,并且可以正常使用。有什么想法可能导致问题吗?

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);
        }
    }

}

最佳答案

您需要重命名

class P100




public class Main


当您将代码复制到UVa时,它会告诉您未找到Main类。这样法官可以运行您的代码(因为Java需要知道类名)。我自己有时会忘记这样做。

09-25 21:19