import java.util.Scanner;
public class 二维数组矩阵相乘 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("输入x1,x2,x3:");
int x1 = s.nextInt();
int x2 = s.nextInt();
int x3 = s.nextInt();
int[][] a= new int[x1][x2];
int[][] b=new int[x2][x3];
System.out.println("输入a矩阵:");
for(int i=0; i<x1; i++)
{
for(int j=0; j<x2; j++)
{
a[i][j] = s.nextInt();
}
System.out.println();
}
System.out.println("输入b矩阵:");
for(int i=0; i<x2; i++)
{
for(int j=0; j<x3; j++)
{
b[i][j] = s.nextInt();
}
System.out.println();
}
int n = 0;
int m = a.length;
for (int i = 0; i < b.length; i++) {
n = b[i].length;
}
int[][] c = new int[m][n];
System.out.println("a矩阵和b矩阵相乘的结果:");
for (int i = 0; i < m; i++) {
for (int k = 0; k < n; k++) {
for (int t = 0; t < b.length; t++) {
c[i][k] += a[i][t] * b[t][k];
}
System.out.print(c[i][k] + " ");
}
System.out.println();
}
}
}
两个矩阵相乘算法
于 2019-06-03 17:03:19 首次发布