import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s; while ((s = br.readLine()) != null) { int[][] a = new int[2][3]; int[][] b = new int[3][2]; String[] ss = s.split(" "); for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { a[i][j] = Integer.parseInt(ss[j]); } ss = br.readLine().split(" "); } for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { b[i][j] = Integer.parseInt(ss[j]); } if (i != 2) ss = br.readLine().split(" "); } int result[][] = MatrixMul(a, b); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { System.out.print(result[i][j] + " "); } System.out.println(); } // int sum = 0; // for (int j = 0; j < 3; j++) { // sum += a[0][j] * b[j][0]; // } // System.out.print(sum + " "); // // sum = 0; // for (int j = 0; j < 3; j++) { // sum += a[0][j] * b[j][1]; // } // System.out.println(sum); // // sum = 0; // for (int j = 0; j < 3; j++) { // sum += a[1][j] * b[j][0]; // } // System.out.print(sum + " "); // // sum = 0; // for (int j = 0; j < 3; j++) { // sum += a[1][j] * b[j][1]; // } // System.out.println(sum); } } private static int[][] MatrixMul(int[][] a, int[][] b) { int[][] result = new int[a.length][a.length]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { for (int k = 0; k < b.length; k++) { result[i][j] += a[i][k] * b[k][j]; } } } return result; } }
import java.util.Scanner; public class Main { public static final int n=2; public static final int m=3; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[][] matrix = new int[n][m]; int[][] matrix1 = new int[m][n]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) matrix[i][j]=scanner.nextInt(); for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) matrix1[i][j]=scanner.nextInt(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int sum=0; for (int k = 0; k < m; k++) sum+=matrix[i][k]*matrix1[k][j]; System.out.print(sum+" "); } System.out.println(); } } }
运行时间:37ms
占用内存:10780k
虽然数组小,但是找规律不可少,锻炼思维很有用
但是就本题来说,暴力解可取。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[][] mat1 = new int[2][3];
int[][] mat2 = new int[3][2];
Scanner scan = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
// 输入第一个矩阵
for (int i = 0; i < mat1.length; i++) {
for (int j = 0; j < mat1[0].length; j++) {
mat1[i][j] = scan.nextInt();
}
}
// 输入第二个矩阵
for (int i = 0; i < mat2.length; i++) {
for (int j = 0; j < mat2[0].length; j++) {
mat2[i][j] = scan.nextInt();
}
}
// 控制输出
int sum = 0;// 记录每次三对数相加的和
/**
* mat1和mat2两个矩阵 mat1[i][j] mat2[k][m]如果这样看待这四个索引 那么找出其运算时候的规律就行了
* 循环操作比起暴力解更可取(虽然本题循环次数不多 暴力解仍可取)
*/
for (int i = 0; i < mat1.length; i++) {// i是 0 1 但实际四次循环i取值为0 0 1 1
for (int j = 0; j < mat1[0].length; j++) {// j是0 1 2循环 一共4次
sum += mat1[i][j] * mat2[j][0];
}
list.add(sum);// 将和sum保存进list中
sum = 0;// 将sum清零 不会影响下面的运算 下同
for (int j = 0; j < mat1[0].length; j++) {// j是0 1 2循环 一共4次
sum += mat1[i][j] * mat2[j][1];
}
list.add(sum);
sum = 0;
}
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
if (i == 1) {// 输出俩数后得换行
System.out.println();
}
}
}
}