题解 | #矩阵乘法#
矩阵乘法
http://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x = Integer.valueOf(scan.nextLine().trim());
int y = Integer.valueOf(scan.nextLine().trim());
int z = Integer.valueOf(scan.nextLine().trim());
int[][] m1 = new int[x][y];
int[][] m2 = new int[y][z];
for (int i = 0; i < x; i++) {
String[] tmp = scan.nextLine().split(" ");
for (int j = 0; j < y; j++) {
m1[i][j] = Integer.valueOf(tmp[j]);
}
}
for (int i = 0; i < y; i++) {
String[] tmp = scan.nextLine().split(" ");
for (int j = 0; j < z; j++) {
m2[i][j] = Integer.valueOf(tmp[j]);
}
}
int[][] ans = new int[x][z];
for (int i = 0; i < x; i++) {
for (int j = 0; j < z; j++) {
int tmp = 0;
for (int k = 0; k < y; k++) {
tmp += m1[i][k] * m2[k][j];
}
ans[i][j] = tmp;
}
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < z; j++) {
if (j == z - 1) {
System.out.println(ans[i][j]);
break;
}
System.out.print(ans[i][j] + " ");
}
}
}
}