-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixMultiplication_607.java
60 lines (53 loc) · 1.85 KB
/
MatrixMultiplication_607.java
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
import java.util.Scanner;
public class MatrixMultiplication_607 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String matIndexes = scanner.nextLine();
String[] matIndexesSplit = matIndexes.split(" ");
int i, j, k;
i = Integer.parseInt(matIndexesSplit[0]);
j = Integer.parseInt(matIndexesSplit[1]);
k = Integer.parseInt(matIndexesSplit[2]);
int[][] matA;
int[][] matB;
matA = readMatrix(i, j, scanner);
matB = readMatrix(j, k, scanner);
printMatrix(matrixMultiplier(matA, matB));
}
private static int[][] matrixMultiplier(int[][] matA, int[][] matB) {
int a, b;
a = matA.length;
b = matB[0].length;
int[][] matrix = new int[a][b];
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
matrix[i][j] = 0;
for (int k = 0; k < matB.length; k++) {
matrix[i][j] += matA[i][k] * matB[k][j];
matrix[i][j] = matrix[i][j];
}
}
}
return matrix;
}
private static void printMatrix(int[][] matrix) {
for (int[] ints : matrix) {
for (int j = 0; j < ints.length; j++) {
System.out.print(ints[j]);
if (j != ints.length - 1) System.out.print(" ");
}
System.out.println();
}
}
private static int[][] readMatrix(int i, int j, Scanner scanner) {
int[][] matrix = new int[i][j];
for (int a = 0; a < i; a++) {
String elementsLine = scanner.nextLine();
String[] elements = elementsLine.split(" ");
for (int b = 0; b < j; b++) {
matrix[a][b] = Integer.parseInt(elements[b]);
}
}
return matrix;
}
}