题解 | #矩阵乘法#

矩阵乘法

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 a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            int [][] A = new int[a][b];
            int [][] B = new int[b][c];
            int [][] C = new int[a][c];
            for (int i = 0 ; i < a ; i ++) {
                for (int j = 0 ; j < b ; j++) {

                    A[i][j] = in.nextInt();

                }


            }

            for (int i = 0 ; i < b ; i ++) {
                for (int j = 0 ; j < c ; j++) {

                    B[i][j] = in.nextInt();

                }


            }

            for (int i = 0 ; i < a ; i ++) {
                for (int j = 0 ; j < c ; j++) {

                    C[i][j] =  0;
                    for (int k = 0 ; k < b ; k++) {
                        C[i][j] +=  A[i][k] * B[k][j];
                    }
                    System.out.print(C[i][j]+" ");
                }
                System.out.println( );

            }





        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务