java矩阵相乘的计算

时间:2023-01-08 22:40:48
package a123;
import java.util.Scanner;
public class a132 {
 public static void main(String args[]) {
  Scanner in = new Scanner(System.in);
  while (in.hasNext()) {
   int m = in.nextInt();
   int n = in.nextInt();
   int n1 = in.nextInt();
   double a[][] = new double[m + 1][n + 1];
   double b[][]= new double[n + 1][n1 + 1];
   double c[][] = new double[m + 1][n1 + 1];
   for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
     a[i][j] = in.nextDouble();
    }
    System.out.println();
   }
   System.out.println();
   System.out.println();
   for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n1; j++) {
     b[i][j] = in.nextDouble();
    }
    System.out.println();
   }
   System.out.println();
   System.out.println();
   for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n1; j++) {
     c[i][j] = 0;
     for (int k = 1; k <= n; k++) {
      c[i][j] += a[i][k] * b[k][j];
     }
     System.out.printf("%10.2f", c[i][j]);
    }
    System.out.println();
   }
  }
 }
}