题解 | #矩阵乘法#

矩阵乘法

https://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b

**package org.example;**

**import java.util.Scanner;**

**/****
** * 创建时间:2022年09月10日17时29分04秒**
** ***
** * @author liangpenghui**
** **/**
**public class HJ69矩阵乘法 {**
**    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();**
**            Matrix m1 = new Matrix(x, y);**
**            Matrix m2 = new Matrix(y, z);**
**            for (int i = 0; i < x; i++) {**
**                for (int j = 0; j < y; j++) {**
**                    m1.arr[i][j]=scanner.nextInt();**
**                }**
**            }**
**            for (int i = 0; i < y; i++) {**
**                for (int j = 0; j < z; j++) {**
**                    m2.arr[i][j]=scanner.nextInt();**
**                }**
**            }**
**            Matrix.ride(m1,m2).print();**

**        }**
**    }**

**    private static class Matrix {**
**        int m;**
**        int n;**
**        int[][] arr;**

**        public void print(){**
**            for (int i = 0; i < m; i++) {**
**                for (int j = 0; j < n; j++) {**
**                    System.out.print(arr[i][j]+" ");**
**                }**
**                System.out.println();**
**            }**
**        }**

**        public Matrix(int m, int n) {**
**            this.m = m;**
**            this.n = n;**
**            this.arr = new int[m][n];**
**        }**

**        public static Matrix ride(Matrix m1, Matrix m2) {**
**            int[][] arr1 = new int[m1.m][m2.n];**
**            for (int i = 0; i < m1.m; i++) {**
**                for (int j = 0; j < m2.n; j++) {**
**                    int s0 = 0;**
**                    for (int k = 0; k < m1.n; k++) {**
**                        s0 += m1.arr[i][k] * m2.arr[k][j];**
**                    }**
**                    arr1[i][j] = s0;**
**                }**
**            }**
**            Matrix matrix = new Matrix(m1.m, m2.n);**
**            matrix.arr = arr1;**
**            return matrix;**
**        }**
**    }**
**}**
#矩阵起源#
全部评论

相关推荐

整顿职场的柯基很威猛:这种不可怕,最可怕的是夹在一帮名校里的二本选手,人家才是最稳的。
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务