12.15(第八天)
矩阵相乘
1 三重循环是多数程序的极限
2 非法输入检查是程序正常运行的基本保障. 如果检查所有的非法输入, 会导致大量代码行, 这在商业代码中是必须的.
3.求解矩阵相乘常用两个办法,蛮力法和分治法,以下使用的主要是蛮力法解决低阶矩阵,分治法较高深,用来解决高阶矩阵,有兴趣可移步(48条消息) Java实现矩阵相乘问题_南 墙-CSDN博客_java矩阵乘法
package basic;
import java.util.Arrays;
public class MatrixMultiplication {
public static void main(String args[]) {
matrixMultiplicationTest();
}
// 实现矩阵相乘的前提条件是前面矩阵的一行与后面矩阵的一列相等
public static int[][] multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix) {
int m = paraFirstMatrix.length;
int n = paraFirstMatrix[0].length;
int p = paraSecondMatrix[0].length;
// 检查后面矩阵一列元素所含元素是否与前面矩阵的相等
if (paraSecondMatrix.length != n) {
System.out.println("The two matrices cannot be multiplied.");
return null;
}
// 结果组成的矩阵就是一个m*p阶方阵,在这里实现矩阵相乘
int[][] resultMatrix = new int[m][p];
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
for (int k = 0; k < n; k++) {
resultMatrix[i][j] += paraFirstMatrix[i][k] * paraSecondMatrix[k][j];
}//蛮力法解决解决低阶矩阵较轻松
}
}
return resultMatrix;
}
// 初始化前两个矩阵,并调用multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix)方法
// 实现矩阵相乘
public static void matrixMultiplicationTest() {
int[][] tempFirstMatrix = new int[2][3];
for (int i = 0; i < tempFirstMatrix.length; i++) {
for (int j = 0; j < tempFirstMatrix[0].length; j++) {
tempFirstMatrix[i][j] = i + j;
}
}
System.out.println("The first matrix is: rn" + Arrays.deepToString(tempFirstMatrix));
int[][] tempSecondMatrix = new int[3][2];
for (int i = 0; i < tempSecondMatrix.length; i++) {
for (int j = 0; j < tempSecondMatrix[0].length; j++) {
tempSecondMatrix[i][j] = i * 10 + j;
}
}
System.out.println("The second matrix is: rn" + Arrays.deepToString(tempSecondMatrix));
int[][] tempThirdMatrix = multiplication(tempFirstMatrix, tempSecondMatrix);
System.out.println("The third matrix is: rn" + Arrays.deepToString(tempThirdMatrix));
// 第一个矩阵乘以自己本身
System.out.println("Trying to multiply the first matrix with itself.rn");
tempThirdMatrix = multiplication(tempFirstMatrix, tempFirstMatrix);
System.out.println("The result matrix is: rn" + Arrays.deepToString(tempThirdMatrix));
}
}
运行结果
The first matrix is: [[0, 1, 2], [1, 2, 3]] The second matrix is: [[0, 1], [10, 11], [20, 21]] The third matrix is: [[50, 53], [80, 86]] Trying to multiply the first matrix with itself. The two matrices cannot be multiplied. The result matrix is: null
今日单词
while语句
1 while 语句本质上比 for 更基础, 因此可以替代后者. 但 for 在很多时候更方便.
2 break 语句又出现了, 上次是在 switch 语句里. 都是表示跳出当前代码块.
这是使用while语句找出的不超过设置值的最大值的例子
package basic;
public class WhileStatement {
public static void main(String args[]) {
whileStatementTest();
}
// 找出没有超过设置值的最大值
public static void whileStatementTest() {
int tempMax = 100;
int tempValue = 0;
int tempSum = 0;
// 方法一
while (tempSum <= tempMax) {
tempValue++;
tempSum += tempValue;
System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);
}
tempSum -= tempValue;
System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);
// 方法二
System.out.println("rnAlternative approach.");
// 将值清零
tempValue = 0;
tempSum = 0;
while (true) {
tempValue++;
tempSum += tempValue;
System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);
if (tempMax < tempSum) {
break;
}
}
tempSum -= tempValue;
System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);
}
}
运行结果
tempValue = 1, tempSum = 1 tempValue = 2, tempSum = 3 tempValue = 3, tempSum = 6 tempValue = 4, tempSum = 10 tempValue = 5, tempSum = 15 tempValue = 6, tempSum = 21 tempValue = 7, tempSum = 28 tempValue = 8, tempSum = 36 tempValue = 9, tempSum = 45 tempValue = 10, tempSum = 55 tempValue = 11, tempSum = 66 tempValue = 12, tempSum = 78 tempValue = 13, tempSum = 91 tempValue = 14, tempSum = 105 The sum not exceeding 100 is: 91 Alternative approach. tempValue = 1, tempSum = 1 tempValue = 2, tempSum = 3 tempValue = 3, tempSum = 6 tempValue = 4, tempSum = 10 tempValue = 5, tempSum = 15 tempValue = 6, tempSum = 21 tempValue = 7, tempSum = 28 tempValue = 8, tempSum = 36 tempValue = 9, tempSum = 45 tempValue = 10, tempSum = 55 tempValue = 11, tempSum = 66 tempValue = 12, tempSum = 78 tempValue = 13, tempSum = 91 tempValue = 14, tempSum = 105 The sum not exceeding 100 is: 91



