题解 | #矩阵乘法#

矩阵乘法

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));
        }
    }
}

全部评论

相关推荐

totoroyyw:千年老妖😂
投递华为等公司10个岗位
点赞 评论 收藏
分享
11-03 14:38
重庆大学 Java
AAA求offer教程:我手都抬起来了又揣裤兜了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务