Вращение квадратной матрицы не работает правильно после первого вращенияJAVA

Программисты JAVA общаются здесь
Anonymous
Вращение квадратной матрицы не работает правильно после первого вращения

Сообщение Anonymous »

Проверьте приведенный ниже код, матч после второго вращения должен быть идентичен целевым массивам TAR . Однако этого не происходит. Не могли бы вы просмотреть, чтобы проверить, где логика идет не так?// input
int[][] mat = {{0, 0, 0}, {0, 1, 0}, {1, 1, 1}};

// **target matrix that should be achieved after second
// rotation of mat, i.e, 180 degree, but isn't working????**
int[][] tar = {{1, 1, 1}, {0, 1, 0}, {0, 0, 0}};

int[][] ans = new int[mat.length][mat.length];

//////First rotation/////////
for (int i = 0; i < mat.length; i++) {
int x = mat.length - 1 - i;
for (int j = 0; j < mat.length; j++) {
ans[j][x] = mat[j];
}
}

mat = ans;

// Second rotation-----------------------------
for (int i = 0; i < mat.length; i++) {
int x = mat.length - 1 - i;
for (int j = 0; j < mat.length; j++) {
ans[j][x] = mat[j];
}
}
//**not matching to target, but it should!!! HELPPPP**
for (int[] e : ans) {
System.out.println(Arrays.toString(e));
}


Подробнее здесь: https://stackoverflow.com/questions/796 ... t-rotation

Вернуться в «JAVA»