矩阵的乘法 按照矩阵乘法规则
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int oneLine = in.nextInt();
int oneLieandNextline = in.nextInt();
int nextLie = in.nextInt();
int[][] A = new int[oneLine][oneLieandNextline];
int[][] B = new int[oneLieandNextline][nextLie];
for (int i = 0; i < oneLine; i++) {
for (int j = 0; j < oneLieandNextline; j++) {
A[i][j] = in.nextInt();
}
}
for (int i = 0; i < oneLieandNextline; i++) {
for (int j = 0; j < nextLie; j++) {
B[i][j] = in.nextInt();
}
}
int[][] result = function(A, B);
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println("");
}
}
}
public static int[][] function(int[][] A, int[][] B) {
int aLen = A.length;
int aLie = A[0].length;
int bLine = B.length;
int bLie = B[0].length;
int[][] result = new int[aLen][bLie];
for (int i = 0; i < aLen; i++) {
for (int j = 0; j < bLie; j++) {
for (int k = 0; k < bLine; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
return result;
}
}