题目描述
计算两个矩阵的乘积,第一个是2*3,第二个是3*2
输入描述:
输入为两个矩阵,其中一个为2*3的矩阵,另一个为3*2的矩阵
输出描述:
一个2*2的矩阵(每一个数字后都跟一个空格)
示例1
输入
1 2 3 3 4 5 6 7 8 9 10 11
输出
52 58 100 112
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int[][] f1 = new int[2][3];
int[][] f2 = new int[3][2];
int[][] r = new int[2][2];
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
f1[i][j] = input.nextInt();
}
}
for(int i=0;i<3;i++){
for(int j=0;j<2;j++){
f2[i][j] = input.nextInt();
}
}
r[0][0] = f1[0][0]*f2[0][0]+f1[0][1]*f2[1][0]+f1[0][2]*f2[2][0];
r[0][1] = f1[0][0]*f2[0][1]+f1[0][1]*f2[1][1]+f1[0][2]*f2[2][1];
r[1][0] = f1[1][0]*f2[0][0]+f1[1][1]*f2[1][0]+f1[1][2]*f2[2][0];
r[1][1] = f1[1][0]*f2[0][1]+f1[1][1]*f2[1][1]+f1[1][2]*f2[2][1];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
System.out.print(r[i][j]+" ");
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int[][] f1 = new int[2][3];
int[][] f2 = new int[3][2];
int[][] r = new int[2][2];
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
f1[i][j] = input.nextInt();
}
}
for(int i=0;i<3;i++){
for(int j=0;j<2;j++){
f2[i][j] = input.nextInt();
}
}
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
for(int k=0;k<3;k++){
r[i][j] += f1[i][k]*f2[k][j];
}
}
}
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
System.out.print(r[i][j]+" ");
}
System.out.println();
}
}
}