publicclassMatrixRotation{ publicstaticvoidmain(String[] args){ Scanner scan = new Scanner(System.in);
int n = scan.nextInt();// n * n matrix int[][] matrix = newint[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { matrix[i][j] = scan.nextInt(); } }
int m = scan.nextInt();// rotate m times int[][] rotatedMatrix = rotate(m, matrix);
for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(rotatedMatrix[i][j]); System.out.print(" "); } System.out.println(); } }
// Rotate matrix, 90 degrees clock wise. publicstaticint[][] rotate(int[][] matrix) { int size = matrix[0].length; int[][] result = newint[size][size];
for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { result[i][j] = matrix[size - 1 - j][i]; } } return result; }
// Rotate matrix for many times. publicstaticint[][] rotate(int times, int[][] matrix) { for (int i = 0; i < times; i++) { matrix = rotate(matrix); } return matrix; } }