题解 | #矩阵乘法#
矩阵乘法
https://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); int[][] matrix1 = new int[x][y]; int[][] matrix2 = new int[y][z]; int[][] ans = new int[x][z]; for (int i = 0; i < matrix1.length; i++) { for (int j = 0; j < matrix1[0].length; j++) { matrix1[i][j] = sc.nextInt(); } } for (int i = 0; i < matrix2.length; i++) { for (int j = 0; j < matrix2[0].length; j++) { matrix2[i][j] = sc.nextInt(); } } for (int i = 0; i < matrix1.length; i++) { for (int k = 0; k < matrix2[0].length; k++) { int sum = 0; for (int j = 0; j < matrix1[0].length; j++) { sum += matrix1[i][j] * matrix2[j][k]; } ans[i][k] = sum; } } for (int[] an : ans) { for (int j = 0; j < ans[0].length; j++) { System.out.print(an[j] + " "); } System.out.println(); } } }