gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:42:29 25 4
gpt4 key购买 nike

我正在解决 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com