1 #include <stdio.h> 2 3 void showDet(int n,int d[n][n]); 4 int getVal(int n,int d[n][n]); 5 int getA(int n,int d[n][n],int x,int y); 6 7 int main(int argc, char *argv[]) 8 { 9 do{ 10 int n,i,j; 11 printf("请输入行列式的阶数(输入0退出):\n"); 12 scanf("%d",&n); 13 if(n==0) break; 14 printf("请输入行列式(空格分隔):\n"); 15 int d[n][n]; 16 for(i=0;i<n;i++){ 17 for(j=0;j<n;j++){ 18 scanf("%d",&d[i][j]); 19 } 20 } 21 printf("\n输入行列式如下:\n"); 22 showDet(n,d); 23 printf("\n该行列式的值为:\n%d\n\n\n",getVal(n,d)); 24 25 }while(1); 26 return 0; 27 } 28 29 void showDet(int n,int d[n][n]){ 30 int i,j; 31 for(i=0;i<n;i++){ 32 for(j=0;j<n;j++){ 33 printf("%d\t",d[i][j]); 34 } 35 printf("\n"); 36 } 37 } 38 39 int getVal(int n,int d[n][n]){ 40 if(n == 1) return d[0][0]; 41 int i,j,sum=0; 42 43 for(i=0;i<n;i++){ 44 sum=sum+d[0][i]*getA(n,d,1,i+1); 45 } 46 return sum; 47 } 48 49 int getA(int n,int d[n][n],int x,int y){ 50 int a[n-1][n-1],i,j; 51 for(i=0;i<n;i++){ 52 if(i==x-1) continue; 53 for(j=0;j<n;j++){ 54 if(j==y-1) continue; 55 else if(i<x-1 && j<y-1){ 56 a[i][j]=d[i][j]; 57 } else if(i<x-1 && j>y-1){ 58 a[i][j-1]=d[i][j]; 59 }else if(i>x-1 && j<y-1){ 60 a[i-1][j]=d[i][j]; 61 }else if(i>x-1 && j>y-1){ 62 a[i-1][j-1]=d[i][j]; 63 } 64 } 65 } 66 int t; 67 t = (x+y)%2 == 0 ? 1:-1; 68 return t*getVal(n-1,a); 69 }
时间: 2024-11-08 08:25:03