HJ69 矩阵乘法

题目:

HJ69 矩阵乘法

题解:

按照题意模拟计算即可。

    public void matrix(int[][] a, int[][] b) {
        int row = a.length;
        int column = b[0].length;
        int[][] c = new int[row][column];

        for (int i = 0; i< row; i++) {
            for (int j = 0; j < column; j++) {
                c[i][j] = calculateSum(a, i, b, j);
            }
        }

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (j == column - 1) {
                    System.out.println(c[i][j]);
                } else {
                    System.out.print(c[i][j] + " ");
                }
            }
        }
    }

    private int calculateSum(int[][] a, int row, int[][] b, int j) {
        int sum = 0;
        for (int i = 0; i < a[row].length; i++) {
            sum += a[row][i] * b[i][j];
        }
        return sum;
    }

时间复杂度:O(n*m)

你可能感兴趣的:(矩阵,华为)