题解 | #矩阵乘法#
矩阵乘法
https://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); scanner.useDelimiter("\n"); while (scanner.hasNextInt()) { int x = scanner.nextInt(); int y = scanner.nextInt(); int z = scanner.nextInt(); int[][] A = new int[x][y]; int[][] B = new int[y][z]; for (int i = 0; i < A.length; i++) { String[] next = scanner.next().split(" "); for (int j = 0; j < A[0].length; j++) { A[i][j] = Integer.parseInt(next[j]); } } for (int i = 0; i < B.length; i++) { String[] next = scanner.next().split(" "); for (int j = 0; j < B[0].length; j++) { B[i][j] = Integer.parseInt(next[j]); } } //int rows = x; //int column = Math.min(single[0].length, res[0].length); //int column = z; 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.print(C[i][j] + " "); } System.out.println(); } //System.out.println(Arrays.deepToString(C)); } } }