题解 | #矩阵乘法#
矩阵乘法
https://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b
import java.util.Scanner; // 矩阵相乘是行和列相乘后求和 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x = in.nextInt(); int y = in.nextInt(); int z = in.nextInt(); int[][] A = new int[x][y]; int[][] B = new int[y][z]; int[][] res = new int[x][z]; for(int i = 0; i < x; i++){ for(int j = 0; j < y; j++){ A[i][j] = in.nextInt(); } } for(int i = 0; i < y; i++){ for(int j = 0; j < z; j++){ B[i][j] = in.nextInt(); } } for(int i = 0; i < x; i++){ for(int j = 0; j < z; j++){ for(int k = 0; k < y; k++){ res[i][j] += A[i][k] * B[k][j]; } } } for(int i = 0; i < x; i++){ for(int j = 0; j < z; j++){ System.out.print(res[i][j] + " "); } System.out.println(); } } }