即使格式化输入后,uva在线判断JAVA运行时错误

时间:2015-04-02 17:52:53

标签: java runtime-error

我正在解决UVA问题。它不断给出运行时错误。我已经修改了我的代码,在读取stackoverflow上类似问题的答案后,以正确的格式输入,但仍然存在运行时错误。

请告诉我的代码有什么问题? 这是问题链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36

Java代码:

class P100 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        long a[] = new long[1000000];
        Scanner sc ;

        String line = "  ";
        while ((line = br.readLine()) != null) {
            sc=new Scanner(line);
            long l,r;
            if(sc.hasNextLong())
             l = sc.nextLong();
            else
                break;
            if(sc.hasNextLong())
            r = sc.nextLong();
            else
                break;
            long i = l, j = r;
            if (l > r) {
                i = r;
                j=l;
            }
            long max = Long.MIN_VALUE;
            for (long k = i; k < r + 1; k++) {
                if (a[(int)k] == 0) {
                    a[(int)k] = countCycleLength(k);
                }
                if (max < a[(int)k]) {
                    max = a[(int)k];
                }
            }
            System.out.println(l + " " + r + " " + max);

        }

    }

    static long countCycleLength(long j) {
        long count = 1;
        while (j != 1) {
            if (j % 2 != 0) {
                j = j * 3 + 1;
                j = j / 2;
                count += 2;
            } else {
                j = j / 2;
                count++;
            }

        }

        return count;
    }

    static long ip(String s) {
        return Long.parseLong(s);
    }

}

1 个答案:

答案 0 :(得分:1)

最后我通过将类的名称从P100更改为 Main 并将for循环的条件更改为k&lt;来接受此代码。 J + 1。

相关问题