计算行列式

import java.util.Scanner;


public class 计算行列式 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [] [] num = new int[n][n];
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
num[i][j]=sc.nextInt();
}
}
System.out.println(f(num,n));
}
public static int f(int num[][],int n){
if(n==1) return num[0][0];
if(n==2) return num[0][0]*num[1][1]-num[0][1]*num[1][0];
int ans = 0;
int tmp[][] =new int[8][8];
for(int i=0;i<n;++i)
{
for(int row=0; row<n-1; ++row)
{
for(int col=0; col<i; ++col)
tmp[row][col] = num[row+1][col];
for(int col=i; col<n-1;++col)
tmp[row][col] = num[row+1][col+1];
}
ans += num[0][i]*f(tmp,n-1)*(i%2==0?1:-1);
}
return ans;
}

}

猜你喜欢

转载自www.cnblogs.com/shiaguang/p/12340290.html