题解 | #矩阵乘法#
矩阵乘法
https://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b
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.hasNext()) { // 注意 while 处理多个 case int x = in.nextInt(); int y = in.nextInt(); int z = in.nextInt(); int a[][] = new int[x][y]; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { a[i][j] = in.nextInt(); } } int b[][] = new int[y][z]; for (int i = 0; i < y; i++) { for (int j = 0; j < z; j++) { b[i][j] = in.nextInt(); } } int c[][] = 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++) { c[i][j] += a[i][k] * b[k][j]; } System.out.printf("%d ", c[i][j]); } System.out.println(); } } } }