Java :: Aufgabe #55
5 Lösungen

Matrizen um 90 Grad drehen
Anfänger - Java
von pocki
- 11.11.2013 um 16:38 Uhr
Erstelle ein Programm, welches als Eingabe eine Matrix beliebiger Größe entgegennimmt und diese dann um 90 Grad dreht.
Beispiel an einer 4x4 Matrix:
Eingabematrix mit den Elementen:
a11 a12 a13 a14
a21 a22 a23 a24
a31 a32 a33 a34
a41 a42 a43 a44
sollte diese Ausgabematrix erzeugen:
a41 a31 a21 a11
a42 a32 a22 a12
a43 a33 a23 a13
a44 a34 a24 a14
Das Programm sollte für unterschiedlich großen und auch mit nicht quadratischen Matrizen funktionieren.
Beispiel an einer 4x4 Matrix:
Eingabematrix mit den Elementen:
a11 a12 a13 a14
a21 a22 a23 a24
a31 a32 a33 a34
a41 a42 a43 a44
sollte diese Ausgabematrix erzeugen:
a41 a31 a21 a11
a42 a32 a22 a12
a43 a33 a23 a13
a44 a34 a24 a14
Das Programm sollte für unterschiedlich großen und auch mit nicht quadratischen Matrizen funktionieren.
Lösungen:

public class Matrix { public static void main(String[] args) { int matrix[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; // 4x4 matrix ausgeben lassen for (int reihe = 0; reihe < matrix.length; ++reihe) { for (int spalte = 0; spalte < matrix[reihe].length; ++spalte) { System.out.print(matrix[reihe][spalte] + "\t"); } System.out.println("\n"); } System.out.println("\n"); System.out.println("\n"); matrix = dreheArray(matrix); for (int reihe = 0; reihe < matrix.length; ++reihe) { for (int spalte = 0; spalte < matrix[reihe].length; ++spalte) { System.out.print(matrix[reihe][spalte] + "\t"); } System.out.println("\n"); } } public static int[][] dreheArray(int[][] array) { int[][] neuesArray = new int[array[0].length][array.length]; for (int i = 0; i < neuesArray.length; i++) { for (int j = 0; j < neuesArray[0].length; j++) { neuesArray[i][j] = array[j][array[j].length - i - 1]; } } return neuesArray; } }
Output:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13

/** * @author ElPapito * @date 09.05.2015 */ public class MatrizenUm90GradDrehen { public static void main(String[] args) { double[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 0, 1, 2 } }; print(a); System.out.println(); double[][] b = rotate90Degree(a); print(b); } public static double[][] rotate90Degree(double[][] matrix) { int newCols = matrix.length; int newRows = matrix[0].length; double[][] newMatrix = new double[newRows][newCols]; for (int i = 0; i < newRows; i++) { for (int j = 0; j < newCols; j++) { newMatrix[i][j] = matrix[newCols - 1 - j][i]; } } return newMatrix; } public static void print(double[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } }

public static ImageChannel turn(ImageChannel ch) { byte[] lum = ch.getIntensity(); int iH = ch.getHeight(); int iW = ch.getWidth(); byte[] v = new byte[lum.length]; for (int i = 0; i < iH; i++) { for (int j = 0; j < iW; j++) { v[j * iH + i] = (byte)lum[i * iW + j]; } } ImageChannel chinv = new ImageChannel(v, iH, iW); return chinv; }

import java.util.Scanner; public class Matrix { private int[][] matrixBelegen(int[][] matrix){ Scanner scan = new Scanner(System.in); int[][] matrixBelegt = matrix; for( int i = 0; i < matrix.length; i++){ for( int u = 0; u < matrix[0].length; u++) { System.out.printf("Geben sie das Element, dass in der Reihe %s und in der Spalte %s sein soll %n", i + 1, u + 1); matrixBelegt[i][u] = scan.nextInt(); } } System.out.printf("\n"); scan.close(); return matrixBelegt; } private int[][] matrixDrehen90(int[][] matrix) { int[][] matrix90 = new int[matrix[0].length][matrix.length]; //Es sollen nur rechteckige Matrizen sein. Wenn auch nicht rechteckige gefordert werden, //bitte ich Sie, mir darüber bescheid zu sagen. for(int i = 0; i < matrix90[0].length; i++) { for( int n = 0 , u = matrix90.length-1; n < matrix90.length; n++){ matrix90[u--][i] = matrix[i][n]; } } matrixAusgeben(matrix90); return matrix90; } public void matrixAusgeben(int[][] matrix){ System.out.println("Matrix: \n"); for(int i = 0; i < matrix.length; i++){ for( int u = 0; u < matrix[0].length; u++){ // matrix[0].length, da es, wie schon gesagt, ein rechteckiges Array sein soll System.out.printf(matrix[i][u] + "\t"); } System.out.println(""); System.out.println(""); } } public static void main( String[] args) { Matrix m = new Matrix(); int[][] matrix = new int[4][2]; int [][] m2 = m.matrixBelegen(matrix); m.matrixAusgeben(matrix); m.matrixDrehen90(m2); } }

import java.util.Scanner; public class matrix { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); int numRow, numColumn; System.out.println("Bitte geben sie die Reihenanzahl an : "); numRow = scanner.nextInt(); System.out.println("Bitte geben sie die Spaltenanzahl an : "); numColumn = scanner.nextInt(); Matrix2D myMatrix = new Matrix2D(numRow, numColumn); myMatrix.fillMatrix(); myMatrix.ouputMatrix(); myMatrix.rotateMatrix(); } }

import java.util.Scanner; public class Matrix2D { int numRow; // Anzahl Zeilen int numColumn; // Anzahl Spalten int [][] mat2D; // unsere Matrix public Matrix2D(int numRow, int numColumn) { this.numRow = numRow; this.numColumn = numColumn; mat2D = new int[numRow][numColumn]; } int [][] fillMatrix() { Scanner scanner = new Scanner(System.in); // loop over 2dimensional matrix and fill it System.out.println("Füllen der matrix : \n"); for(int i = 0; i < numRow; i++) { for(int k = 0; k < numColumn; k++) { System.out.println("Reihe : " + i + "Spalte : " + k); mat2D[i][k] = scanner.nextInt(); } } return mat2D; } void ouputMatrix () { System.out.println("Aktuelle Matrix : \n"); // output every number of matrix for(int i = 0; i < numRow; i++) { for(int m = 0; m < numColumn; m++) { System.out.print(mat2D[i][m] + " "); } System.out.println(); } } void rotateMatrix() { // rotate Matrix 90 degrees to south **** int [][] matrixTmp = new int[numColumn][numRow]; for(int j = 0; j < numRow; j++) { for(int h = 0; h < numColumn; h++) { matrixTmp[h][j] = mat2D[j][h]; } } // *************************************** // now output the rotated Matrix System.out.println("\nRotierte Matrix : \n"); for(int j = 0; j < numColumn; j++) { for(int h = 0; h < numRow; h++) { System.out.print(matrixTmp[j][h] + " ") ; } System.out.println(); } } }
// Ausgabe : *******************************//
Bitte geben sie die Reihenanzahl an :
3
Bitte geben sie die Spaltenanzahl an :
4
Füllen der matrix :
Reihe : 0Spalte : 0
1
Reihe : 0Spalte : 1
2
Reihe : 0Spalte : 2
3
Reihe : 0Spalte : 3
4
Reihe : 1Spalte : 0
1
Reihe : 1Spalte : 1
2
Reihe : 1Spalte : 2
3
Reihe : 1Spalte : 3
4
Reihe : 2Spalte : 0
1
Reihe : 2Spalte : 1
2
Reihe : 2Spalte : 2
3
Reihe : 2Spalte : 3
4
Aktuelle Matrix :
1 2 3 4
1 2 3 4
1 2 3 4
Rotierte Matrix :
1 1 1
2 2 2
3 3 3
4 4 4