首页 > 试题广场 >

矩阵乘法

[编程题]矩阵乘法
  • 热度指数:80143 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
如果A是个x行y列的矩阵,B是个y行z列的矩阵,把A和B相乘,其结果将是另一个x行z列的矩阵C。这个矩阵的每个元素是由下面的公式决定的

矩阵的大小不超过100*100

输入描述:
第一行包含一个正整数x,代表第一个矩阵的行数
第二行包含一个正整数y,代表第一个矩阵的列数和第二个矩阵的行数
第三行包含一个正整数z,代表第二个矩阵的列数
之后x行,每行y个整数,代表第一个矩阵的值
之后y行,每行z个整数,代表第二个矩阵的值



输出描述:
对于每组输入数据,输出x行,每行z个整数,代表两个矩阵相乘的结果
示例1

输入

2
3
2
1 2 3
3 2 1
1 2
2 1
3 3

输出

14 13
10 11

说明

1 2 3
3 2 1 
乘以
1 2
2 1
3 3
等于
14 13
10 11    
示例2

输入

16
8
7
17 19 16 19 14 1 14 9 
7 2 7 9 16 14 16 12 
13 3 3 17 5 9 8 16 
1 14 16 10 13 13 14 1 
13 13 15 4 7 2 6 16 
16 15 5 5 15 13 1 11 
11 5 0 16 14 7 7 15 
0 16 4 7 16 6 0 15 
2 14 11 2 17 17 5 12 
8 13 11 10 1 17 10 8 
15 16 17 15 7 8 13 14 
5 19 11 3 11 14 5 4 
9 16 13 11 15 18 0 3 
15 3 19 9 5 14 12 3 
9 8 7 11 18 19 14 18 
12 19 9 1 0 18 17 10 
5 18 16 19 6 12 5 
1 17 1 5 9 16 3 
14 16 4 0 19 3 6 
11 9 15 18 11 17 13 
5 5 19 3 16 1 12 
12 13 19 1 10 5 18 
19 18 6 18 19 12 3 
15 11 6 5 10 17 19 

输出

1020 1490 1063 1100 1376 1219 884
966 1035 1015 715 1112 772 920
822 948 888 816 831 920 863
855 1099 828 578 1160 717 724
745 1076 644 595 930 838 688
635 1051 970 600 880 811 846
748 879 952 772 864 872 878
526 722 645 335 763 688 748
764 996 868 362 1026 681 897
836 1125 785 637 940 849 775
1082 1476 996 968 1301 1183 953
609 987 717 401 894 657 662
700 1083 1022 527 1016 746 875
909 1162 905 722 1055 708 720
1126 1296 1240 824 1304 1031 1196
905 1342 766 715 1028 956 749
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            int z = scanner.nextInt();
            int[][] a = new int[x][y];
            int[][] b = new int[y][z];
            int[][] c = new int[x][z];
            for (int i = 0; i < x; i++) {
                for (int j = 0; j < y; j++) {
                    a[i][j] = scanner.nextInt();
                }
            }
            for (int i = 0; i < y; i++) {
                for (int j = 0; j < z; j++) {
                    b[i][j] = scanner.nextInt();
                }
            }
            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();
            }

        }
    }
}

发表于 2024-04-10 20:36:10 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
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 [][] m = 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 < a.length; i++){
            for(int j = 0; j < z; j++){
                for(int k = 0; k < y; k++){
                    m[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(m[i][j]+" ");
            }
            System.out.println();
        }
    }
}

发表于 2023-11-27 14:33:06 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int x = in.nextInt();
            int y = in.nextInt();
            int z = in.nextInt();
            int[][] A = new int[x][y];
            int[][] B = new int[y][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();
                }
            }
            StringBuilder sb = new StringBuilder();
            for(int i=0; i<x; i++){
                for(int j=0; j<z; j++){
                    int sum = 0;
                    for(int k=0; k<y; k++){
                        sum += A[i][k] * B[k][j];
                    }
                    sb.append(sum+" ");
                }
                System.out.println(sb.toString().trim());
                sb.setLength(0);
            }
        }
    }
}

发表于 2023-11-09 10:22:46 回复(0)
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];
        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();
            }
        }

        //存放结果
        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]);
                if(j<z-1){
                    System.out.print(" ");
                }else{
                    System.out.print("\n");
                }
            }
        }
    }

}

发表于 2023-09-08 20:06:40 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
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];
        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++){
            String res = "";
            for(int j = 0;j< z;j++){
                int tag = 0;
                for(int k=0;k<y;k++){
                    tag+= a[i][k]*b[k][j];
                }
                res +=tag+" ";
            }
            System.out.println(res);
        }
    }
}
发表于 2023-08-28 17:27:56 回复(0)
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while ((line = br.readLine()) != null) {
            int hang = Integer.parseInt(line);
            int lie = Integer.parseInt(br.readLine());
            int hang2 = Integer.parseInt(br.readLine());

            int[][] arr1 = new int[hang][lie];
            for (int i = 0; i < hang; i++) {
                String line1 = br.readLine();
                String[] split = line1.split(" ");
                for (int j = 0; j < lie; j++) {
                    arr1[i][j] = Integer.parseInt(split[j]);
                }
            }

            int[][] arr2 = new int[lie][hang2];
            for (int i = 0; i < lie; i++) {
                String line1 = br.readLine();
                String[] split = line1.split(" ");
                for (int j = 0; j < hang2; j++) {
                    arr2[i][j] = Integer.parseInt(split[j]);
                }
            }

            // 最终结果
            int[][] result = new int[hang][hang2];

            for (int i = 0; i < hang; i++) {
                for (int k = 0; k < hang2; k++) {
                    for (int j = 0; j < lie; j++) {
                        //System.out.println("i=" + i + ",k=" + k + ",j =" + j);
                        result[i][k] += arr1[i][j] * arr2[j][k];
                    }
                }

            }

            // 打印结果
            for (int i = 0; i < result.length; i++) {
                for (int j = 0; j < result[i].length; j++) {
                    System.out.print(result[i][j] + " ");
                }
                System.out.println();
            }


        }
    }
}

发表于 2023-08-11 20:43:44 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            int x=Integer.parseInt(sc.nextLine());
            int y=Integer.parseInt(sc.nextLine());
            int z=Integer.parseInt(sc.nextLine());
            int[][] in1=new int[x][y];
            int[][] in2=new int[y][z];
            for(int i=0;i<x;i++){
                String[] str=sc.nextLine().split(" ");
                for(int j=0;j<y;j++){
                    in1[i][j]=Integer.parseInt(str[j]);
                }
            }
            for(int i=0;i<y;i++){
                String[] str=sc.nextLine().split(" ");
                for(int j=0;j<z;j++){
                    in2[i][j]=Integer.parseInt(str[j]);
                }
            }
            int[][] innew=juzhen(in1,in2);
            for(int i=0;i<innew.length;i++){
                for(int j=0;j<innew[0].length;j++){
                    System.out.print(innew[i][j]+" ");
                }
                System.out.println();
            }
        }
    }

    public static int[][] juzhen(int[][] in1,int[][] in2){
        int len1=in1.length;
        int len11=in1[0].length;
        int len2=in2.length;
        int len22=in2[0].length;
        int[][] innew=new int[len1][len22];
        if(len11==len2){
            for(int i=0;i<len1;i++){
                for(int j=0;j<len22;j++){
                    int nums=0;
                    for(int k=0;k<len11;k++){
                        nums+=(in1[i][k]*in2[k][j]);
                    }
                    innew[i][j]=nums;
                }
            }
            return innew;
        }
        return innew;
    }
}

发表于 2023-07-18 18:14:19 回复(0)
import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while((str=br.readLine())!=null){
            int x = Integer.parseInt(str.trim());
            int y = Integer.parseInt(br.readLine().trim());
            int z = Integer.parseInt(br.readLine().trim());
            int[][] matrixA=new int[x][y];
            int[][] matrixB=new int[y][z];
            int[][] matrixC=new int[x][z];
            String[] strArr;
            //给矩阵A赋值
            for(int i=0;i<x;i++){
                strArr=br.readLine().trim().split(" ");
                for(int j=0;j<strArr.length;j++){
                    matrixA[i][j]=Integer.parseInt(strArr[j]);
                }
            }
            //给矩阵B赋值
            for(int i=0;i<y;i++){
                strArr=br.readLine().trim().split(" ");
                for(int j=0;j<strArr.length;j++){
                    matrixB[i][j]=Integer.parseInt(strArr[j]);
                }
            }
            //给矩阵C赋值,第i行j列元素(Cij)的值等于矩阵A第i行
            //元素分别与矩阵B第j行元素分别相乘并求和
            for(int i=0;i<x;i++){
                for(int j=0;j<z;j++){
                    //矩阵A每行有y个元素,矩阵B每列有y个元素,需要进行y次相乘求和
                    for(int k=0;k<y;k++){
                        matrixC[i][j]+=matrixA[i][k]*matrixB[k][j];
                    }
                }
            }
            StringBuilder sb= new StringBuilder();
            String tempStr;
            for(int i=0;i<x;i++){
                for(int j=0;j<z;j++){
                    sb.append(matrixC[i][j]+" ");
                }
                //去除尾部多余的空格
                sb.deleteCharAt(sb.lastIndexOf(" "));
                //换行
                sb.append("\n");
            }
            System.out.println(sb.toString());
        }
    }
}


发表于 2023-07-03 18:41:15 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()){
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            /*
            矩阵A 行数为 a  列数为 b
            矩阵B 行数为 b  列数为 c
            矩阵C 行数为 a  列数为 c
             */
            int[][] arrA = new int[a][b];
            int[][] arrB = new int[b][c];
            //初始化A矩阵
            for (int i = 0; i < arrA.length; i++) {
                for (int j = 0; j < arrA[0].length; j++) {
                    arrA[i][j]=in.nextInt();
                }
            }
            //初始化B矩阵
            for (int i = 0; i < arrB.length; i++) {
                for (int j = 0; j < arrB[0].length; j++) {
                    arrB[i][j]=in.nextInt();
                }
            }
            //C矩阵打印输出 行数为a,列数为b
            for(int i = 0;i<a;i++){
                for (int j = 0; j < c; j++) {
                    System.out.print(sum(arrA, arrB, i,j)+" ");
                }
                System.out.println();
            }
        }
    }
    public static int sum(int[][] arrA,int[][] arrB,int i,int j){
        int sum =0;
        for(int k = 0 ; k<arrA[0].length;k++){
            //根据公式 C[i][j] = sum(A[i][k]*B[k][j] )
            sum+= arrA[i][k]*arrB[k][j];
        }
        return sum;
    }
}

发表于 2023-05-01 17:14:03 回复(0)
import java.util.* ;
import java.lang.Integer;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            int x = Integer.parseInt(sc.nextLine());
            int y = Integer.parseInt(sc.nextLine());
            int z = Integer.parseInt(sc.nextLine());
            int [][] matrix1 = new int [x][y]; //创建一个[x,y]的matrix
            int [][] matrix2 = new int [y][z]; //创建一个[y,z]的matrix
            int [][] resultMatrix = new int [x][z]; //结果矩阵

            //输出第一个矩阵 [x,y]
            for (int i = 0 ; i < x ; i ++) {
                // 一矩阵第一行  y 列
                String [] numString1 = sc.nextLine().split(" ");
                int [] nums1 = new int [y] ;
                for (int j = 0; j < y ; j++) {
                    nums1[j] = Integer.parseInt(numString1[j].trim());
                    matrix1[i][j] = nums1[j];
                }
            }
            //同理可输出第二个矩阵 [y,z]
            for (int i = 0 ; i < y ; i ++) {
                //二矩阵第一行 z列
                String [] numString2 = sc.nextLine().split(" ");
                int [] nums2 = new int [z] ;
                for (int j = 0 ; j < z ; j++) {
                    nums2[j] = Integer.parseInt(numString2[j].trim());
                    matrix2[i][j] = nums2[j];
                }
            }
            //结果矩阵置零
            for (int i = 0 ; i < x ; i++) {
                for (int j = 0; j < z; j++) {
                    resultMatrix[i][j] = 0 ;
                }
            }
            //计算:
            for (int i = 0; i < x ; i++) {
                for (int j = 0 ; j < z ; j++) {
                    for (int k = 0 ; k < y ; k++) {
                        resultMatrix[i][j] = matrix1[i][k] * matrix2[k][j] + resultMatrix[i][j];
                    }

                }
            }
            //输出结果
            for (int i = 0 ; i < x ; i ++) {
                for (int j = 0 ; j < z ; j ++) {
                    System.out.print(resultMatrix[i][j] + " ");
                }
                System.out.print("\n");
            }

        }
    }
}

发表于 2023-02-07 15:23:05 回复(0)
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.hasNextLine()) { // 注意 while 处理多个 case
            int x = Integer.parseInt(in.nextLine());
            int y = Integer.parseInt(in.nextLine());
            int z = Integer.parseInt(in.nextLine());
            int i = 0, j = 0;
            int[][] m1 = new int[x][y], m2 = new int[y][z];
            String line = "";
            for (i = 0; i < x; i++) {
                line = in.nextLine();
                String[] arr = line.split(" ");
                for (j = 0; j < y; j++) {
                    m1[i][j] = Integer.parseInt(arr[j]);
                }
            }
            for (i = 0; i < y; i++) {
                line = in.nextLine();
                String[] arr = line.split(" ");
                for (j = 0; j < z; j++) {
                    m2[i][j] = Integer.parseInt(arr[j]);
                }
            }
            int[][] result = matrixMultiply(m1, m2);
            printMatrix(result);
        }
    }

    public static int[][] matrixMultiply(int[][] m1, int[][] m2) {
        int x = m1.length, y = m1[0].length, z = m2[0].length;
        int[][] result = new int[x][z];
        int i = 0, j = 0, k = 0, sum = 0;
        for (i = 0; i < x; i ++) {
            for (j = 0; j < z; j++) {
                sum = 0;
                for (k = 0; k < y; k++) {
                    sum += m1[i][k] * m2[k][j];
                }
                result[i][j] = sum;
            }
        }
        return result;
    }

    public static void printMatrix(int[][] matrix) {
        int x = matrix.length, y = matrix[0].length, i = 0, j = 0;
        for (i = 0; i < x; i++) {
            for (j = 0; j < y; j++) {
                System.out.print(matrix[i][j]);
                if (j < y - 1) {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

发表于 2023-01-30 22:41:16 回复(0)
//二维数组即可解决,思路清晰
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int h = sc.nextInt();
            int hc = sc.nextInt();
            int c = sc.nextInt();
            int[][] a1 = new int[h][hc];
            int[][] a2 = new int[hc][c];
            for(int i=0;i<h;i++){
                for(int j=0;j<hc;j++){
                    a1[i][j] = sc.nextInt();
                }
            }
            for(int i=0;i<hc;i++){
                for(int j=0;j<c;j++){
                    a2[i][j] = sc.nextInt();
                }
            }
            int[][] aaa=ss(a1,a2);
            for(int i=0;i<aaa.length;i++){
                for(int j=0;j<aaa[0].length;j++){
                    System.out.print(aaa[i][j]+" ");
                }
                System.out.println();
            }
        }
    }

    public static int[][] ss(int[][] a1,int[][] a2){
        int[][] abc = new int[a1.length][a2[0].length];
        int sum=0;
        for(int i=0;i<a1.length;i++){
            for(int j=0;j<a2[0].length;j++){
                for(int k=0;k<a2.length;k++){
                    sum = sum+a1[i][k]*a2[k][j];
                }
                abc[i][j] = sum;
                sum=0;
            }
        }
        return abc;
    }
}
发表于 2022-11-27 20:43:24 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int x = in.nextInt();
            int y = in.nextInt();
            int z = in.nextInt();
            int[][] nums1 = new int[x][y];
            int[][] nums2 = new int[y][z];
            for (int i = 0; i < x; i++) {
                for (int j = 0; j < y; j++) {
                    nums1[i][j] = in.nextInt();
                }
            }

            for (int i = 0; i < y; i++) {
                for (int j = 0; j < z; j++) {
                    nums2[i][j] = in.nextInt();
                }
            }

            int result = 0;
            for (int i = 0; i < x; i++) {
                for (int j = 0; j < z; j++) {
                    result = 0;
                    for (int k = 0; k < y; k++) {
                        result += nums1[i][k] * nums2[k][j];
                    }
                    System.out.print(result + " ");
                }
                System.out.println();
            }
        }
    }
}

发表于 2022-10-21 13:53:33 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Integer x = in.nextInt();
        Integer y = in.nextInt();
        Integer z = in.nextInt();
        // 矩阵一 x行y列
        int[][] one = new int[x][y];
        // 矩阵二 y行z列
        int[][] two = new int[y][z];
        // 根据输入值填充矩阵
        fillArray(one, x, y, in);
        fillArray(two, y, z, in);
        // 结果集矩阵
        int[][] result = new int[x][z];

        // 当前值
        Integer sum = 0;
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < z; j++) {
                for (int k = 0; k < y; k++) {
                    sum += one[i][k] * two[k][j];
                }
                // 填充当前值
                result[i][j] = sum;
                // 重置当前值
                sum = 0;
            }
        }
        // 打印结果
        for (int i = 0; i < x; i++) {
            for (int j = 0 ; j < z; j++) {
                System.out.print(result[i][j] + " ");
            }
            System.out.println();
        }
    }

    /**
     * 根据输入填充矩阵
     */
    private static void fillArray(int[][] array, int row, int column, Scanner in) {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                array[i][j] = in.nextInt();
            }
        }
    }
}

发表于 2022-10-06 14:44:17 回复(0)
在接收第二个矩阵的时候就直接转置,一次接收一列。
运行时间:32ms
超过95.52% 用Java提交的代码
占用内存:10220KB
超过96.59% 用Java提交的代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int x = Integer.parseInt(br.readLine()), y = Integer.parseInt(br.readLine()), z = Integer.parseInt(br.readLine());
        int[][] matrixA = new int[x][y], matrixB = new int[z][y];
        for (int i = 0; i < x; i++) {
            String[] parts = br.readLine().split(" ");
            int index = 0;
            for (String part : parts) {
                matrixA[i][index++] = Integer.parseInt(part);
            }
        }
        for (int j = 0; j < y; j++) {
            String[] parts = br.readLine().split(" ");
            int index = 0;
            for (String part : parts) {
                matrixB[index++][j] = Integer.parseInt(part);
            }
        }

        Solution sl = new Solution();
        System.out.println(sl.multiTwoMatrix(matrixA, matrixB, x, y, z));
    }
}

class Solution {
    public StringBuilder multiTwoMatrix(int[][] matrixA, int[][] matrixB, int x, int y, int z) {
        int[][] answer = new int[x][z];
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < z; j++) {
                int sum = 0;
                for (int k = 0; k < y; k++) {
                    sum += matrixA[i][k] * matrixB[j][k];
                }
                answer[i][j] = sum;
            }
        }
        
        StringBuilder sb = new StringBuilder();
        for (int[] row : answer) {
            for (int num : row) {
                sb.append(num).append(" ");
            }
            sb.append("\n");
        }
        return sb;
    }
}



发表于 2022-09-05 00:14:11 回复(0)
import java.util.Scanner;

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[][] arr01 = new int[x][y];
        int[][] arr02 = new int[y][z];
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                arr01[i][j] = sc.nextInt();
            }
        }
        for (int i = 0; i < y; i++) {
            for (int j = 0; j < z; j++) {
                arr02[i][j] = sc.nextInt();
            }
        }

        int[][] newArr = new int[x][z];
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < z; j++) {
                newArr[i][j] = 0;
                for (int k = 0; k <= y - 1; k++) {
                    newArr[i][j] += (arr01[i][k] * arr02[k][j]);
                }
            }
        }
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < z; j++) {
                System.out.print(newArr[i][j]+" ");
            }
            System.out.println();
        }
    }
}

发表于 2022-07-24 20:49:38 回复(0)