|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
public class Methods { public static int[] invert(int[] array) { int[] invertiert = new int[array.length]; for (int i = 0; i < invertiert.length; i++) invertiert[i] = array[array.length - i - 1]; return invertiert; } public static int[] cut(int[] array, int length) { int[] geschnitten = new int[length]; for (int i = 0; i < geschnitten.length && i < array.length; i++) geschnitten[i] = array[i]; return geschnitten; } public static int[] linearize(int[][] array) { int length = 0; for (int i = 0; i < array.length; i++) length += array[i].length; int[] linearisiert = new int[length]; int linIndex = 0; for (int i = 0; i < array.length; i++) for (int j = 0; j < array[i].length; j++) linearisiert[linIndex++] = array[i][j]; return linearisiert; } public static void print(int[] array) { String tmp = "{" + array[0]; for (int i = 1; i < array.length; i++) { tmp += ", "; tmp += array[i]; } tmp += "}"; System.out.println(tmp); } public static void main(String[] args) { int[] array = new int[] {1, -1, 5, -27, 3, 200, 10}; int[] invertedArray = invert(array); print(invertedArray); int[] cutArray = cut(array, 3); print(cutArray); int[][] multiDimArray = new int[][] {new int[] {1, 3}, new int[] {25}, new int[] {7, 4, 6, 9}}; int[] linearizedArray = linearize(multiDimArray); print(linearizedArray); } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
public class Matrix2D { public static int vecVecMul(int[] v1, int[] v2) { int res = 0; for (int i = 0; i < v1.length; i++) res += v1[i] * v2[i]; return res; } public static int[] matVecMul(int[][] m, int[] v) { int[] resvec = new int[m.length]; for (int i = 0; i < resvec.length; i++) resvec[i] = vecVecMul(m[i], v); return resvec; } public static int[][] transpose(int[][] m) { int[][] resmat = new int[m[0].length][m.length]; for (int i = 0; i < resmat.length; i++) for (int j = 0; j < resmat[0].length; j++) resmat[i][j] = m[j][i]; return resmat; } public static int[][] matMatMul(int[][] a, int b[][]) { int[][] resmat = new int[a.length][]; int[][] transposed = transpose(b); for (int i = 0; i < resmat.length; i++) resmat[i] = matVecMul(a, transposed[i]); return transpose(resmat); } public static void printMat(int[][] resmat) { for (int i = 0; i < resmat.length; i++) { System.out.print("| "); for (int j = 0; j < resmat[0].length; j++) System.out.print(resmat[i][j] + " "); System.out.println("|"); } } public static void main(String[] args) { int[][] matrix1 = {{1, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; int[][] matrix2 = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; int[][] resmat = matMatMul(matrix1, matrix2); printMat(matrix1); System.out.println("*"); printMat(matrix2); System.out.println("="); printMat(resmat); } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
public class Mensch extends Aerger { public static void main(String[] args) { int[] spieler1 = {-1, -2, -3, -4}; int[] spieler2 = {-1, -2, -3, -4}; int[] arrRed = {-1, -2, -3, -4}; int[] arrGreen = {-1, -2, -3, -4}; int[] startPositionen = {0, 10, 20, 30}; // StartPositionen der Spieler int winner = -1; int wurf; int player = 1; String playerFarbe = ""; int stoneToMove; int neuePosition; // Game Loop while (winner == -1) { paintField(spieler1, spieler2, arrRed, arrGreen); switch (player) { case 1: playerFarbe = "Gelb"; break; case 2: playerFarbe = "Blau"; break; default: break; } writeLineConsole("Es ist " + playerFarbe + " an der Reihe."); wurf = dice(); writeLineConsole(playerFarbe + " hat eine " + wurf + " gewuerfel!"); stoneToMove = -1; neuePosition = -1; while (stoneToMove < 1 || stoneToMove > 4) { while (stoneToMove < 1 || stoneToMove > 4) stoneToMove = read("Welchen Stein m枚chten Sie vorr眉cken? 0 zum "); if (player == 1) { if (spieler1[stoneToMove - 1] < 0) neuePosition = startPositionen[player - 1] + wurf; else if (spieler1[stoneToMove - 1] >= 0 && spieler1[stoneToMove - 1] <= 39) neuePosition = (spieler1[stoneToMove - 1] + wurf) % 40; else neuePosition = spieler1[stoneToMove - 1] + wurf; } else { if (spieler2[stoneToMove - 1] < 0) neuePosition = startPositionen[player - 1] + wurf; else if (spieler2[stoneToMove - 1] >= 0 && spieler2[stoneToMove - 1] <= 39) neuePosition = (spieler2[stoneToMove - 1] + wurf) % 40; else neuePosition = spieler2[stoneToMove - 1] + wurf; } if (player == 1 && neuePosition < 6 && spieler1[stoneToMove - 1] > 30) neuePosition += 40; else if (player == 2 && neuePosition < 16 && spieler2[stoneToMove - 1] < 10 && spieler2[stoneToMove - 1] >= 0) neuePosition += 40; // pr眉fen ob die neue Position frei ist bzw. ob man jemand schlagen darf // check die neue Position wenn eigener Stein da steht --> blockiert // gegnerische Stein -> wird geschlagen if (player == 1) { int index = 0; while (index < 4) { if (spieler1[index] == neuePosition) stoneToMove = -1; if (spieler2[index] == neuePosition) spieler2[index] = -1; index++; } } else { int index = 0; while (index < 4) { if (spieler2[index] == neuePosition) stoneToMove = -1; if (spieler1[index] == neuePosition) spieler1[index] = -1; index++; } } } if (player == 1) spieler1[stoneToMove - 1] = neuePosition; else spieler2[stoneToMove - 1] = neuePosition; // hat jemand gewonnen... int index = 0; winner = 1; while (index < 4) { if (spieler1[index] < 40) winner = -1; index++; } if (winner == 1) { writeLineConsole("Spieler 1 hat gewonnen (" + playerFarbe + ")"); return; } else { winner = 2; index = 0; while (index < 4) { if (spieler2[index] < 40) winner = -1; index++; } } if (winner == 2) { writeLineConsole("Spieler 2 hat gewonnen (" + playerFarbe + ")"); return; } // spieler wechseln if (player == 1) player = 2; else player = 1; } } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
import java.io.DataInputStream; import java.io.IOException; public class GrausGauß { public static int[] readMatrix() throws IOException { // Simple Implementierung, die einfach nur nacheinander die Koeffizienten // per read() einliest, ist auch ok. DataInputStream in = new DataInputStream(System.in); outer: while (true) { int[] matrix = new int[lines * columns()]; for (int i = 0; i < lines; i++) { @SuppressWarnings("deprecation") String nextLine = in.readLine(); String[] lineElements = nextLine.trim().split("\\s+"); if (lineElements.length != columns()) { System.out.println("Ung眉ltige Eingabe."); continue outer; } for (int j = 0; j < columns(); j++) try { set(matrix, i, +j, Integer.parseInt(lineElements[j])); } catch (NumberFormatException __) { System.out.println("Ung眉ltige Eingabe."); continue outer; } } return matrix; } } public static int lines = 3; public static int get(int[] matrix, int line, int column) { return matrix[line * columns() + column]; } public static void set(int[] matrix, int line, int column, int value) { matrix[line * columns() + column] = value; } public static int columns() { return lines + 1; } public static void multLine(int[] matrix, int line, int factor) { // System.out.println("matrix[" + line + "] *= " + factor); for (int column = 0; column < columns(); column++) set(matrix, line, column, get(matrix, line, column) * factor); } public static void multAddLine(int[] matrix, int line, int otherLine, int factor) { // System.out.println("matrix[" + line + "] += matrix[" + otherLine + "]*" + factor); for (int column = 0; column < columns(); column++) set(matrix, line, column, get(matrix, line, column) + get(matrix, otherLine, column) * factor); } public static void printMatrix(int[] matrix) { for (int i = 0; i < lines; i++) { for (int j = 0; j < columns(); j++) { System.out.print(get(matrix, i, j) + "\t"); } System.out.println(); } } public static void swap(int[] matrix, int lineA, int lineB) { // System.out.println("swap line " + lineA + " with line " + lineB); for (int column = 0; column < columns(); column++) { int temp = get(matrix, lineA, column); set(matrix, lineA, column, get(matrix, lineB, column)); set(matrix, lineB, column, temp); } } public static void searchSwap(int[] matrix, int fromLine) { // System.out.println("searchSwap line " + fromLine + " in column " + column); for (int line = fromLine; line < lines; line++) { int column = fromLine; int e = get(matrix, line, column); if (e == 0) continue; if (line == fromLine) return; swap(matrix, fromLine, line); return; } } public static int ggt(int a, int b) { if (b > a) { int temp = b; b = a; a = temp; } while (b != 0) { int temp = b; b = a % b; a = temp; } return a; } public static int kgv(int a, int b) { int ggt = ggt(a, b); // In a*b ist der gr枚脽te gemeinsame Teiler 2x vorhanden; daher muss 1x // durch den ggT geteilt werden. return (a / ggt) * b; } public static int[] rowEchelonToResult(int[] matrix) { int[] result = new int[lines]; // Wir gehen von unten nach oben durch die Matrix, weil wir auf diese // Weise auf die Werte der x_i, die wir jeweils brauchen, vorher // schon berechnet haben. // // Variablen sind ausnahmsweise zur Verdeutlichung in 'Snake Case'. for (int x_i = lines - 1; x_i >= 0; x_i--) { result[x_i] = get(matrix, x_i, columns() - 1); // Wir holen die anderen x_i 'mit Minus auf die andere Seite'. for (int x_i_other = x_i + 1; x_i_other < lines; x_i_other++) result[x_i] += -get(matrix, x_i, x_i_other) * result[x_i_other]; int divisor = get(matrix, x_i, x_i); if(result[x_i] % divisor != 0) // Erzeugung von Exceptions war hier nicht gefordert throw new RuntimeException("L枚sung nicht ganzzahlig."); result[x_i] /= divisor; } return result; } public static int[] solveSystem(int[] matrix) { // Wir gehen spaltenweise von links nach rechts durch die Matrix. for (int column = 0; column < lines; column++) { // Die Referenzzeile jeder Spalte ist die Zeile des Diagonalenelements (Pivot) int refLine = column; // Ist das Pivot gleich 0, muss getauscht werden. searchSwap(matrix, refLine); // int pivot = get(matrix, refLine, column); if (pivot == 0) // Erzeugung von Exceptions war hier nicht gefordert throw new RuntimeException("Gleichungssystem nicht eindeutig l枚sbar."); // Wir nullifizieren (TM) alle Elemente unter dem Pivot durch Anwendung der Formel // auf dem Blatt. for (int zeroLine = refLine + 1; zeroLine < lines; zeroLine++) { int belowPivot = get(matrix, zeroLine, column); if (belowPivot == 0) continue; int kgv = kgv(pivot, belowPivot); multLine(matrix, zeroLine, kgv / belowPivot); multAddLine(matrix, zeroLine, refLine, -kgv / pivot); } } System.out.println("Matrix in Zeilenstufenform:"); printMatrix(matrix); return rowEchelonToResult(matrix); } public static void main(String[] args) throws IOException { // int[] matrix = {1, 2, 3, 2, 1, 1, 1, 2, 3, 3, 1, 0}; int[] matrix = {0, -1, -2, 0, 1, 2, 3, 2, 3, 3, 1, 0}; // int matrix[] = readMatrix(); System.out.println("Matrix:"); printMatrix(matrix); int[] result = solveSystem(matrix); System.out.println("Ergebnis:"); for (int i = 0; i < result.length; i++) { System.out.println("x" + (i + 1) + " = " + result[i]); } } } |