在Java中能够手动输入两个矩阵,用分治法求这两个矩阵相乘的代码
时间: 2024-02-13 16:07:59 浏览: 68
好的,以下是Java代码实现:
```java
import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入矩阵A的行数:");
int m1 = scanner.nextInt();
System.out.print("请输入矩阵A的列数:");
int n1 = scanner.nextInt();
System.out.print("请输入矩阵B的行数:");
int m2 = scanner.nextInt();
System.out.print("请输入矩阵B的列数:");
int n2 = scanner.nextInt();
if (n1 != m2) {
System.out.println("矩阵A的列数必须等于矩阵B的行数!");
return;
}
int[][] A = new int[m1][n1];
int[][] B = new int[m2][n2];
System.out.println("请输入矩阵A:");
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n1; j++) {
A[i][j] = scanner.nextInt();
}
}
System.out.println("请输入矩阵B:");
for (int i = 0; i < m2; i++) {
for (int j = 0; j < n2; j++) {
B[i][j] = scanner.nextInt();
}
}
int[][] C = matrixMultiplication(A, B);
System.out.println("矩阵A和矩阵B的乘积为:");
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n2; j++) {
System.out.print(C[i][j] + " ");
}
System.out.println();
}
}
private static int[][] matrixMultiplication(int[][] A, int[][] B) {
int m = A.length;
int n = A[0].length;
int p = B[0].length;
int[][] C = new int[m][p];
if (m == 1 && n == 1 && p == 1) {
C[0][0] = A[0][0] * B[0][0];
} else {
int[][] A11 = new int[m/2][n/2];
int[][] A12 = new int[m/2][n/2];
int[][] A21 = new int[m/2][n/2];
int[][] A22 = new int[m/2][n/2];
int[][] B11 = new int[n/2][p/2];
int[][] B12 = new int[n/2][p/2];
int[][] B21 = new int[n/2][p/2];
int[][] B22 = new int[n/2][p/2];
divideMatrix(A, A11, A12, A21, A22);
divideMatrix(B, B11, B12, B21, B22);
int[][] C11 = matrixAddition(matrixMultiplication(A11, B11), matrixMultiplication(A12, B21));
int[][] C12 = matrixAddition(matrixMultiplication(A11, B12), matrixMultiplication(A12, B22));
int[][] C21 = matrixAddition(matrixMultiplication(A21, B11), matrixMultiplication(A22, B21));
int[][] C22 = matrixAddition(matrixMultiplication(A21, B12), matrixMultiplication(A22, B22));
mergeMatrix(C, C11, C12, C21, C22);
}
return C;
}
private static void divideMatrix(int[][] A, int[][] A11, int[][] A12, int[][] A21, int[][] A22) {
int m = A.length;
int n = A[0].length;
for (int i = 0; i < m/2; i++) {
for (int j = 0; j < n/2; j++) {
A11[i][j] = A[i][j];
}
}
for (int i = 0; i < m/2; i++) {
for (int j = n/2; j < n; j++) {
A12[i][j-n/2] = A[i][j];
}
}
for (int i = m/2; i < m; i++) {
for (int j = 0; j < n/2; j++) {
A21[i-m/2][j] = A[i][j];
}
}
for (int i = m/2; i < m; i++) {
for (int j = n/2; j < n; j++) {
A22[i-m/2][j-n/2] = A[i][j];
}
}
}
private static void mergeMatrix(int[][] C, int[][] C11, int[][] C12, int[][] C21, int[][] C22) {
int m = C.length;
int p = C[0].length;
for (int i = 0; i < m/2; i++) {
for (int j = 0; j < p/2; j++) {
C[i][j] = C11[i][j];
}
}
for (int i = 0; i < m/2; i++) {
for (int j = p/2; j < p; j++) {
C[i][j] = C12[i][j-p/2];
}
}
for (int i = m/2; i < m; i++) {
for (int j = 0; j < p/2; j++) {
C[i][j] = C21[i-m/2][j];
}
}
for (int i = m/2; i < m; i++) {
for (int j = p/2; j < p; j++) {
C[i][j] = C22[i-m/2][j-p/2];
}
}
}
private static int[][] matrixAddition(int[][] A, int[][] B) {
int m = A.length;
int n = A[0].length;
int[][] C = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
return C;
}
}
```
以上代码实现了矩阵乘法的分治法算法,可以手动输入两个矩阵,输出它们的乘积。
阅读全文