我们在计算行列式的时候,是不是有个苦恼,要是有个计算机能帮你算,那可真是太好了,因为我们平时遇到的题,行列式的阶数也不是特别多,所以我自己就写了一个,利用行列式无限展开,直到变成一阶,最后在回溯,通过这样的递归求得结果。
运行结果图
代码的话我都放在下面:
package 行列式;
public class app {
public static void main(String[] args) {
new xuanze();
}
}
package 行列式;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class xuanze extends JFrame {
JButton j1,j2,j3,j4,j5,j6;
int p=3;
public xuanze() {
this.setLayout(new GridLayout(6, 1));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(100,600);
j1=new JButton("三阶");
j2=new JButton("四阶");
j3=new JButton("五阶");
j4=new JButton("六阶");
j5=new JButton("七阶");
j6=new JButton("八阶");
j1.addActionListener((e)->{
dispose();
jsview jsview = new jsview(3);
});
j2.addActionListener((e)->{
dispose();
jsview jsview = new jsview(4);
});
j3.addActionListener((e)->{
dispose();
jsview jsview = new jsview(5);
});
j4.addActionListener((e)->{
dispose();
jsview jsview = new jsview(6);
});
j5.addActionListener((e)->{
dispose();
jsview jsview = new jsview(7);
});
j6.addActionListener((e)->{
dispose();
jsview jsview = new jsview(8);
});
this.add(j1);this.add(j2);this.add(j3);
this.add(j4);this.add(j5);this.add(j6);
}
}
package 行列式;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class jsview extends JFrame {
int n;JTextField[] wu;
int[][] wuu;
public jsview(int n) {
this.n=n;
if(n==3) {
wu=new JTextField[9];
wuu=new int[3][3];
}
if(n==4) {
wu=new JTextField[16];
wuu=new int[4][4];
}
if(n==5) {
wu=new JTextField[25];
wuu=new int[5][5];
}
if(n==6) {
wu=new JTextField[36];
wuu=new int[6][6];
}
if(n==7) {
wu=new JTextField[49];
wuu=new int[7][7];
}
if(n==8) {
wu=new JTextField[64];
wuu=new int[8][8];
}
pp();
}
int jie;JButton a=new JButton("计算");
JButton b=new JButton("清空");
int s=0;
JTextField o=new JTextField("此处显示结果");
public void pp() {
this.setLayout(new GridLayout(n+1, n));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize((n+1)*80, n*80);
for(int i=0;i<wu.length;i++) {
wu[i]=new JTextField("");
this.add(wu[i]);
}
this.add(a);
this.add(o);
this.add(b);
a.addActionListener((e)->{
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
int v=Integer.valueOf(wu[s].getText());
wuu[i][j]=v;
s++;
}
}
aij aaAij=new aij();
jie=aaAij.det(wuu);
o.setText(""+jie);
});
b.addActionListener((e)->{
dispose();
new jsview(n);
});
}
}
package 行列式;
class aij {
// A函数可用于求余子阵
public int[][] A(int[][] a, int row, int column) {
int[][] ans = new int[a.length - 1][a.length - 1];// ans用于储存返回的最终结果
int[] temp = new int[(a.length - 1) * (a.length - 1)];// 临时一维数组temp用于按顺序储存剔除相应行和列元素后的数组
int k = 0;
// 剔除行和列并按顺序储存到temp内
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (i == row - 1) {
continue;
} else if (j == column - 1) {
continue;
}
temp[k++] = a[i][j];
}
}
// 按顺序从temp中读取数据并储存到ans内
k = 0;
for (int i = 0; i < ans.length; i++) {
for (int j = 0; j < ans[i].length; j++) {
ans[i][j] = temp[k++];
}
}
return ans;
}
// det用于求行列式
public int det(int[][] a) {
if (a.length == 1) {
return a[0][0];
} else {
int ans=0;
for (int i = 0; i < a.length; i++) {
ans+=a[i][0]*(int)Math.pow(-1, i)*det(A(a,i+1,1));
}
return ans;
}
}
}
看完希望大家点个赞哟!!!