近段时间Java所有作业、实验、万维都可以从DWLLLL/Java_Homework (github.com)上下载源码,无需任何注册。
进入后点击Code
选择Download ZIP
以压缩包的形式下载后,解压就可以得到所有源代码了。
第六题
所有矩阵的题目用二维数组实现都很简单,只有矩阵的逆比较麻烦,具体代码如下(建议从上边直接下载源代码,包含更多的注释和解释)
主类
import cn.com.my.Matrix;
public class Test {
public static void main(String[] args) {
int[][] matrix1 = {{2, 3, 4}, {5, 6, 7}};
int[][] matrix2 = {{1, 2}, {3, 4}};
int[][] matrix5 = {{1, 2}, {-1, -3}};
int[][] matrix11 = {{1, 2, 3}, {4, 5, 6}};
int[][] matrix3 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] matrix4 = {{1, 2, -1}, {3, 1, 0}, {-1, -1, -2}};
Matrix matrixMultiplication = new Matrix();
int[][] matrixMultiply = matrixMultiplication.matrixMultiply(matrix1, matrix2);
matrixMultiplication.printMatrix(matrixMultiply);
int[][] matrixAddition = matrixMultiplication.matrixAddition(matrix1, matrix11);
matrixMultiplication.printMatrix(matrixAddition);
int[][] matrixSubtraction = matrixMultiplication.matrixSubtraction(matrix1, matrix11);
matrixMultiplication.printMatrix(matrixSubtraction);
int[][] matrixTranspose = matrixMultiplication.matrixTranspose(matrix1);
matrixMultiplication.printMatrix(matrixTranspose);
int[][] matrixInverse = matrixMultiplication.matrixInverse(matrix5);
matrixMultiplication.printMatrix(matrixInverse);
}
}
Matrix类
package cn.com.my;
public class Matrix {
public int[][] matrixMultiply(int[][] a, int[][] b) {
if (a[0].length != b.length) {
return null;
}
int[][] c = new int[a.length][b[0].length];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b[0].length; j++) {
for (int k = 0; k < a[0].length; k++)
c[i][j] += b[k][j] * a[i][k];
}
}
return c;
}
public int[][] matrixAddition(int[][] a, int[][] b) {
if (a[0].length != b[0].length || a.length != b.length) {
return null;
}
int[][] c = new int[a.length][a[0].length];
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
c[i][j] = a[i][j] + b[i][j];
return c;
}
public int[][] matrixSubtraction(int[][] a, int[][] b) {
if (a[0].length != b[0].length || a.length != b.length) {
return null;
}
int[][] c = new int[a.length][a[0].length];
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
c[i][j] = a[i][j] - b[i][j];
return c;
}
public int matrixMinor(int[][] a,int i,int j) {
int[][] c = new int[a.length-1][a.length-1];
int b = 0;
if (a.length != a[0].length) {
return 0;
} else if (a.length == 1) {
return 0;
} else {
for (int i1 = 0; i1 < a.length; i1++) {
for (int j1 = 0, j2 = 0; j1 < a.length - 1; j1++, j2++) {
for (int k1 = 0, k2 = 0; k1 < a[0].length - 1; k1++, k2++) {
if (j2 == i)
j2++;
if (k2 == j)
k2++;
c[j1][k1] = a[j2][k2];
}
}
b = determinant(c);
}
if((i+j)%2==1)
return -b;
else
return b;
}
}
public int determinant(int[][] a){
int[] b=new int[a.length];
int[][] c=new int[a.length-1][a[0].length-1];
int sum=0;
if (a.length != a[0].length) {
return 0;
} else if (a.length == 1) {
return a[0][0];
}else if (a.length == 2){
return (a[0][0]*a[1][1])-(a[0][1]*a[1][0]);
}else {
for (int i = 0;i < a.length;i++){
for (int j1 = 0,j2 = 1;j1 < a.length-1;j1++,j2++){
for (int k1 = 0,k2 = 0;k1 < a[0].length-1;k1++,k2++) {
if(k2 == i)
k2++;
c[j1][k1]=a[j2][k2];
}
}
b[i]=a[0][i]*determinant(c);
if (i%2 == 1)
sum -= b[i];
else
sum += b[i];
}
return sum;
}
}
public int[][] matrixAdjugate(int[][] a){
int[][] c = new int[a.length][a[0].length];
for (int i = 0; i < a.length; i++){
for (int j = 0; j < a[i].length; j++){
c[i][j] = matrixMinor(a, j, i);
}
}
return c;
}
public int[][] matrixInverse(int[][] a){
if(a.length != a[0].length)
return null;
int[][] c = new int[a.length][a[0].length];
int[][] d = new int[a.length][a[0].length];
int b = determinant(a);
d=matrixAdjugate(a);
if(b==0) {
for (int i = 0; i < d.length; i++) {
for (int j = 0; j < d[0].length; j++) {
c[i][j] = 0;
}
}
}
else{
for (int i = 0;i < d.length;i++){
for(int j = 0;j < d[0].length;j++){
c[i][j] = d[i][j] / b;
}
}
}
return c;
}
public int[][] matrixTranspose(int[][] a) {
int[][] c = new int[a[0].length][a.length];
for (int i = 0; i < a[0].length; i++){
for (int j = 0; j < a.length; j++){
c[i][j] = a[j][i];
}
}
return c;
}
public void printMatrix(int[][] c) {
if (c != null) {
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[0].length; j++) {
System.out.printf("%d ", c[i][j]);
}
System.out.println();
}
} else {
System.out.println("无效");
}
System.out.println();
}
}
第七题
主类
public class Test {
public static void main(String[] args){
MatrixLocation matrixLocation = new MatrixLocation();
double[][] a = {{1.2,1.3,2.4},{2.1,5.2,6.2},{1.3,2.5,6.1}};
matrixLocation.location(a);
}
}
MatrixLocation类
public class MatrixLocation {
int row;
int column;
double maxValue=0;
public void location(double[][] a){
for (int i = 0; i < a.length; i++){
for (int j = 0; j < a[0].length; j++){
if (maxValue < a[i][j]){
maxValue = a[i][j];
row = i;
column = j;
}
}
}
System.out.println("最大值是:"+maxValue);
System.out.println("位置是:"+"第"+row+"行"+"第"+column+"列");
}
}