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

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

}

最佳答案

最后,我通过将类名从 P100 更改为 Main 并将 for 循环的条件更改为 k < j+1 来接受此代码。

关于即使在格式化输入后,uva在线判断中的JAVA运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29419339/

相关文章:

java - 对 Collection<V> 和 Set<K> 进行排序

java - 数组/链表 : performance depends on the *direction* of traversal?

asp.net - 为什么我在此 aspx 页面中看不到详细的错误消息?

java - 需要帮助查找错误

CUDA double 和每个线程的寄存器数量

java - Dagger 2 零场注入(inject)

java - 使用基于 Java 的配置在服务器模式下设置 H2

java - JDBC 许可 - 将 jdbc 打包在 jar 中

android - Android图像原像处理中的C Jni错误

c++ - 通过函数读取文件时出现问题