我一直在运行这个算法来解决这个 CCC(加拿大计算机竞赛)问题。它运行良好,并在 IntelliJ 上给出正确的输出,但在 DMOJ 和 CCC 在线评分器中显示 NoSuchElementException。
这是我的代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
solution(sc.nextInt());
}
public static void solution(int lines) {
Scanner sc = new Scanner(System.in);
int[][] sunflowers = new int[lines][lines];
int[][] temp = new int[lines][lines];
// Creating sunflowers array
for (int i = 0; i < lines; i++) {
for (int j = 0; j < lines; j++) {
sunflowers[i][j] = sc.nextInt();
}
}
boolean readyToSubmit = false;
int a = 0;
int b = 0;
while (readyToSubmit == false) {
b = 0;
a = 0;
readyToSubmit = true;
for (int g = 0; g < sunflowers.length - 1; g++) {
for (int h = 1; h < sunflowers[g].length; h++) {
if (sunflowers[g][h - 1] > sunflowers[g][h]) {
// Turn true if previous value smaller than current
readyToSubmit = false;
}
}
}
// If each column is in descending order
for (int d = 0; d < sunflowers.length; d++) {
for (int e = 1; e < sunflowers.length; e++) {
if (sunflowers[e - 1][d] > sunflowers[e][d]) {
readyToSubmit = false;
}
}
}
if (readyToSubmit == true) {
break;
}
// Rotating the Array w/ temp
for (int i = sunflowers.length - 1; i >= 0; i--) { // we want position to go right -> left
b = 0;
for (int j = 0; j < sunflowers[0].length; j++) { // We want columns to go up -> down
temp[a][b] = sunflowers[j][i];
b += 1;
}
a += 1;
}
for (int x = 0; x < lines; x++) {
for (int y = 0; y < lines; y++) {
sunflowers[x][y] = temp[x][y];
}
}
}
for (int s = 0; s < sunflowers.length; s++) {
for (int k = 0; k < sunflowers[s].length; k++) {
System.out.print(sunflowers[s][k] + " ");
}
System.out.println();
}
}
}
输入: 3 3 7 9 2 5 6 1 3 4
输出(在 IntelliJ 中):
1 2 3
3 5 7
4 6 9
输出(在 DMOJ 上): IR(java.util.NoSuchElementException)
输出(在 CCC Grader 上):
Exception in thread "main" java.util.NoSuchElementException
<251 more characters> // unfortunately, I am not able to see what the 251 characters are.
我目前不确定导致此 NoSuchElementException 的原因(因为它没有告诉我 DMOJ 或 CCC 分级机上的行号)。任何帮助将不胜感激。
最佳答案
注意:这是在评论部分找到的,我只是将其添加为答案以验证此问题是否已解决。
删除此行 Scanner sc = new Scanner(System.in);
关于solution
方法。然后关闭sc
之后solution(sc.nextInt())
;放在 main 方法上。引用这个[link] 1
通过 solution
中的扫描仪方法。更改您的solution
接受扫描仪的方法,因此方法签名将为 public static void solution(int lines, Scanner sc)
,然后在你的 main 方法中通过 solution(sc.nextInt(), sc);
调用它。然后solution(sc.nextInt(), sc);
之后使用 sc.close()
关闭扫描仪
关于java - CCC 2018 J4 : NoSuchElementException Error (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54779302/