【母函数】母函数前提知识:矩阵相乘

A=2*3
1 2 3
4 5 6

B=3*4
1 2 3 4
5 6 7 8
4 3 2 1

R = A*B

思路:A矩阵第一行乘B矩阵第一列得等R矩阵的第一行第一列的值,以此类推。
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
        
        
        
        
import java.util.* ;
public class MatrixMultiply {
public static void main ( String args []){
Scanner sc = new Scanner ( System . in );
int m = sc . nextInt ();
int n = sc . nextInt ();
int o = sc . nextInt ();
int p = sc . nextInt ();
int [][] a = new int [ m ][ n ];
int [][] b = new int [ o ][ p ];
int [][] r = new int [ m ][ p ];
//输入矩阵a 2行3列 a[0]= 1 2 3
// a[1]= 4 5 6
for ( int i = 0 ; i < a . length ; i ++){
for ( int j = 0 ; j < a [ i ]. length ; j ++){
a [ i ][ j ]= sc . nextInt ();
}
}
//输入矩阵b
for ( int i = 0 ; i < b . length ; i ++){
for ( int j = 0 ; j < b [ i ]. length ; j ++){
b [ i ][ j ]= sc . nextInt ();
}
}
//矩阵相乘
for ( int i = 0 ; i < a . length ; i ++){ //A矩阵按行走
for ( int j = 0 ; j < b [ i ]. length ; j ++){ //B矩阵按列走
//计算r[i][j]
r [ i ][ j ]= 0 ;
for ( int k = 0 ; k < b . length ; k ++){
r [ i ][ j ] = r [ i ][ j ]+ a [ i ][ k ]* b [ k ][ j ];
}
}
}
Magic . print2 ( r );//这是一个输出函数,可以自己写
}
}

你可能感兴趣的:(【母函数】母函数前提知识:矩阵相乘)