首页 > 编程 > Java > 正文

JAVA实现矩阵连乘问题

2019-11-08 02:18:48
字体:
来源:转载
供稿:网友
public class MatrixMultiply { public static int[][] matrixMultiply(int a[][], int b[][]) { int c[][] = new int[a.length][b[0].length]; if (a[0].length == b.length) { // 控制循环次数 for(int i=0;i<a.length;i++){ for (int j = 0; j < b[0].length; j++) { int sum = 0; for (int k = 0; k < a.length; k++) { sum = sum + a[i][k] * b[k][j]; System.out.PRintln(a[i][k]+" * "+b[k][j]+"----->"+sum); } c[i][j]=sum; } } } else { System.out.println("矩阵不能相乘!"); } return null; } public static void main(String[] args) { // TODO Auto-generated method stub int a[][] = { { 1, 2, 3 }, { 4, 5, 6 } }; int b[][] = { { 1, 2 }, { 3, 4 }, { 5, 6 } }; MatrixMultiply .matrixMultiply(a, b); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表