题解 | #矩阵乘法#
矩阵乘法
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 = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
int[][] arr1 = new int[x][y];
int[][] arr2 = new int[y][z];
//给第一个矩阵赋值
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
arr1[i][j]=sc.nextInt();
}
}
//给第二个矩阵赋值
for(int i=0;i<y;i++){
for(int j=0;j<z;j++){
arr2[i][j]=sc.nextInt();
}
}
int[][] mul = mulSum(arr1,arr2,x,y,z);
//打印mul
for(int i=0;i<x;i++){
for(int j=0;j<z;j++){
System.out.print(mul[i][j]+" ");
}
System.out.println();
}
}
}
public static int[][] mulSum(int[][] arr1, int[][] arr2,int x,int y,int z){
int[][] mul = new int[x][z];
for(int i=0;i<x;i++){
for(int j=0;j<z;j++){
for(int k=0;k<y;k++){
mul[i][j] += arr1[i][k]*arr2[k][j];
}
}
}
return mul;
}
}