题解 | #矩阵乘法#
矩阵乘法
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.hasNextInt()) { // 注意 while 处理多个 case // 接收二维数组长度 int x = in.nextInt(); int y = in.nextInt(); int z = in.nextInt(); // 定义二维数组 int[][] xy = new int[x][y]; int[][] yz = new int[y][z]; int[][] xz = new int[x][z]; // 获取二维数组值 for (int i = 0; i < x ; i++) { for (int j = 0; j < y; j++) { xy[i][j] = in.nextInt(); } } for (int i = 0; i < y ; i++) { for (int j = 0; j < z; j++) { yz[i][j] = in.nextInt(); } } // 向新的二维数组写入数据 for (int i = 0 ; i < x ; i++) { for (int k = 0 ; k < z ; k++) { int j = 0 ; while(j<y){ xz[i][k] += xy[i][j] * yz[j][k]; j++; } } } for (int i = 0 ; i < x ; i++) { for (int j = 0 ; j < z ; j++) { System.out.print(xz[i][j] + " "); } System.out.println(); } } } }