HJ69 矩阵乘法

题目:

HJ69 矩阵乘法

题解:

按照题意模拟计算即可。

java 复制代码
    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)

相关推荐
遇到困难睡大觉哈哈9 小时前
HarmonyOS —— Remote Communication Kit 拦截器(Interceptor)高阶定制能力笔记
笔记·华为·harmonyos
遇到困难睡大觉哈哈10 小时前
HarmonyOS —— Remote Communication Kit 定制处理行为(ProcessingConfiguration)速记笔记
笔记·华为·harmonyos
遇到困难睡大觉哈哈10 小时前
HarmonyOS支付接入证书准备与生成指南
华为·harmonyos
BlackWolfSky11 小时前
鸿蒙暂未归类知识记录
华为·harmonyos
L、21813 小时前
Flutter 与开源鸿蒙(OpenHarmony):跨平台开发的新未来
flutter·华为·开源·harmonyos
L、21814 小时前
Flutter 与 OpenHarmony 深度融合实践:打造跨生态高性能应用(进阶篇)
javascript·flutter·华为·智能手机·harmonyos
遇到困难睡大觉哈哈14 小时前
HarmonyOS —— Remote Communication Kit 定制数据传输(TransferConfiguration)实战笔记
笔记·华为·harmonyos
FrameNotWork15 小时前
【HarmonyOS 状态管理超全解析】从 V1 到 V2,一次讲清 @State/@Observed/@Local…等所有装饰器!附超易懂示例!
华为·harmonyos
5008415 小时前
鸿蒙 Flutter 隐私合规:用户授权中心与数据审计日志
flutter·华为·开源·wpf·音视频