UPD20190318
我发现写错了…交换行的时候没写变号…
----------------
上课闲着没事…用Java写了个求行列式的
还是高斯消元那样求
我的命名还是一如既往的风骚233
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| import java.util.Scanner; class QAQ { private double eps = 1e-6; private int n; private double[][] a = new double[55][55];
private boolean cmp(double a, double b) { return Math.abs(a-b)<eps; } public void init() { Scanner sn = new Scanner(System.in); n = sn.nextInt(); for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { a[i][j] = sn.nextDouble(); } } } public double Solve() { double ans = 1; for(int i=1; i<=n; i++) { if(cmp(a[i][i],0)) { ans = -ans; int flag = 0; for(int j=i+1; j<=n; j++) if(!cmp(a[j][i],0)) flag = j; if(flag==0) return 0; for(int j=i; j<=n; j++) { double t=a[i][j]; a[i][j]=a[flag][j]; a[flag][j]=t; } } for(int j=i+1; j<=n; j++) { double t=a[j][i]/a[i][i]; for(int k=i; k<=n; k++) a[j][k]-=t*a[i][k]; } } for(int i=1; i<=n; i++) ans*=a[i][i]; return ans; } public void print() { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { System.out.print(a[i][j] + " "); }System.out.println(""); } } public static void main(String[] args) { QAQ qwq = new QAQ(); qwq.init(); System.out.println("ans = " + qwq.Solve()); qwq.print(); } }
|