题解 | #矩阵乘法#
矩阵乘法
http://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b
import java.util.*; public class Main{ public static void main(String [] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int x = Integer.parseInt(sc.nextLine()); int y = Integer.parseInt(sc.nextLine()); int z = Integer.parseInt(sc.nextLine()); int [][] arr1 = new int[x][y]; int [][] arr2 = new int[y][z]; int [][] res = new int[x][z];
for (int i = 0; i < x; i++) {
String [] strs = sc.nextLine().split(" ");
for (int j = 0; j < y; j++)
{
arr1[i][j] = Integer.parseInt(strs[j]);
}
}
for (int i = 0; i < y; i++) {
String [] strs = sc.nextLine().split(" ");
for (int j = 0; j < z; j++) {
arr2[i][j] = Integer.parseInt(strs[j]);
}
}
//第一个矩阵的行分别乘以第二个矩阵的列
for (int k = 0; k < z; k++) {//第二矩阵的列数
for (int i = 0; i < x; i++) {//第一矩阵的行数
int xsum = 0;
for (int j = 0; j < y; j++) {//第一矩阵的列数、第二矩阵的行数
xsum += arr1[i][j] * arr2[j][k];
}
res[i][k] = xsum;
}
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < z; j++) {
System.out.print(res[i][j] + " ");
}
System.out.println();
}
}
}
}