以下是我用全排列编写的行列式计算程序,貌似出了点问题,每次输出的sum值都为0.请各位高手指点哪里出了问题???
#include<stdio.h>
#include <math.h>
int a[3][3]={};
double sum;
void main()
{double per(int* a, int i, int n);
int ni(int ,int ,int );
for(int b=0;b<=2;b++)
for(int c=0;c<=2;c++)
{printf("请输入%d",a[b][c]);
scanf("%d",&a[b][c]); }
for(int i=0;i<=2;i++)
{for(int g=0;g<=2;g++)
{printf("%d ",a[i][g]);}
printf("\n");}
getchar();
double sum1=0;
int y[] = {0,1,2};
sum1 =per(y, 0, 3);
printf("%d",sum1);
}
int ni(int a ,int b,int c )
{int d=0;int n[3]={a,b,c};
for(int z=0;z<=2;z++)
for (int f=0;f<=z;f++)
if ( n[z]<n[f])
d=d+1;
return(d);
}
void swap(int* a, int* b)
{
int t;
t = *a; *a = *b; *b = t;
}
double per(int* h, int i, int n)
{
int k;
int g;
int f[3]={0};
if(i == n) {
int x=h[0],c=h[1],v=h[2];
f[0]=a[x][0];
f[1]=a[c][1];
f[2]=a[v][2];
g=ni(f[0],f[1],f[2]);
sum=pow(-1.0,g)*f[0]*f[1]*f[2]+sum;
}
else
{for(k = i; k < n; ++k)
{swap(h+i, h+k);
per(h, i+1, n);
swap(h+i, h+k);
}
}
return (sum);}