【发布时间】:2020-09-09 14:32:38
【问题描述】:
我想为奇数矩阵实现 Strauss 算法。为了解决这个问题,我需要在底部添加一行零,并在最后一列之后添加一列新的零。如何在我的代码中做到这一点?
例如: int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int[][] matrix2 = {{3, 2, 1}, {6, 5, 4}, {9, 8, 7}};
should be converted to
int[][] matrix1 = {{1, 2, 3, 0}, {4, 5, 6, 0}, {7, 8, 9, 0}, {0, 0, 0, 0}};
int[][] matrix2 = {{3, 2, 1, 0}, {6, 5, 4, 0}, {9, 8, 7, 0}, {0, 0, 0, 0}};
This is my current code for odd matrices:
The code for even matrix dimensions works.
public static int[][] strassenGeneral(int[][] m1, int[][] m2) {
int n = m1.length;
if (n % 2 == 0) {
strassen(m1, m2);
} else {
n += 1;
int newresult[][] = new int[n][n];
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n; j++) {
newresult[i][n - 1] = 0;
newresult[n - 1][j] = 0;
}
}
}
return strassen(m1, m2);
}
Thank you very much.
【问题讨论】: