题解 | #矩阵乘法#
矩阵乘法
http://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b
public class Main {
public static void main(String []args) {
Scanner in = new Scanner(System.in);
int m1 = Integer.parseInt(in.nextLine());
int n = Integer.parseInt(in.nextLine());
int m2 = Integer.parseInt(in.nextLine());
int [][] a = new int[m1][n];
int [][] b = new int[n][m2];
for (int i = 0; i < m1; i++) {
String []strs = in.nextLine().split(" ");
for (int j = 0; j < n; j++) {
a[i][j] = Integer.parseInt(strs[j]);
}
}
for (int i = 0; i < n; i++) {
String []strs = in.nextLine().split(" ");
for (int j = 0; j < m2; j++) {
b[i][j] = Integer.parseInt(strs[j]);
}
}
int[][] c = new int[m1][m2];
ArrayList<Integer> res = new ArrayList();
for (int i = 0; i < m1; i++) {
int index = 0;
while (index < m2) {
int sum = 0;
for (int j = 0; j < n; j++) {
sum += a[i][j] * b[j][index];
}
index++;
res.add(sum);
}
}
for (int i = 0; i < res.size(); i++) {
if (i % m2 == 0 && i > 0) {
System.out.println();
}
System.out.print(res.get(i) + " ");
}
}
}