Я использовал этот алгоритм, чтобы решить этот вопрос CCC (Canadian Computing Contest).Он работает нормально и дает правильный вывод на IntelliJ, но показывает исключение NoSuchElementException в DMOJ и онлайн-грейдере CCC.
Вот мой код:
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).Любая помощь будет принята с благодарностью.