简单的Java题,有关矩阵乘法

凑个字数6666666666666666666666666666666666666666666666666666666666666

img

import java.util.*;
class Solution {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        Map<Integer,int[]> map = new HashMap<>();
        for(int i = 1;i <= num;i++){
            int[] temp = new int[2];
            temp[0] = sc.nextInt();//行数
            temp[1] = sc.nextInt();//列数
            map.put(i,temp);
        }
        Map<Integer,int[][]> matrix = new HashMap<>();
        for(int i = 1;i <= num;i++){
            int[] temp1 = map.get(i);
            int rows = temp1[0], clos = temp1[1];
            int[][] arr = new int[rows][clos];
            for(int j = 0;j < rows;j++){
                for(int k = 0;k < clos;k++){
                    arr[j][k] = sc.nextInt();
                }
            } 
            matrix.put(i, arr);
        }
        sc.close();
        //开始遍历map进行矩阵乘法
        int[][] res = multiply(matrix.get(1),matrix.get(2));
        for(int i = 3;i <= num;i++){
            res = multiply(res, matrix.get(i));
        }
        for(int i = 0;i < res.length;i++){
            for(int j = 0;j < res[0].length;j++){
                System.out.print(res[i][j] + " ");
            }
            System.out.println();
        }
    }
    public static int[][] multiply(int[][] m1, int[][] m2){
        int[][] res = new int[m1.length][m2[0].length];
        for(int i=0;i<m1.length;i++) {
            for(int j=0;j<m2[0].length;j++) {
                for(int k=0;k<m2.length;k++) {
                    res[i][j] += (m1[i][k] * m2[k][j]) % 514329;
                }
            }
        }
        return res;
    }
}

请看下面的 ‘相关推荐’, 就有很多 Java 矩阵乘法的介绍了。