行列式求值

简单的行列式求值的模版

题目:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1035

下面是代码:

#include 
#define maxn 11
#define e 1e-6
double c[maxn][maxn];

bool zero(double a)
{
  return a > -e && a < e;
}

double det(int n)
{
  double q,ans = 1;
  int i,j,k;
  for(i = 0; i < n; i++)
  {
    if(zero(c[i][i]))
    {
      for(j = i + 1; j < n; j++)
      {
	if(!zero(c[j][i])) break;
      }
      if(j == k) return 0;
      ans *= -1;
      for(k = i; k < n; k++)
      {
	double temp = c[j][k];
	c[j][k] = c[i][k];
	c[i][k] = temp;
      }
    }
    ans *= c[i][i];
    for(j = i + 1; j < n; j++)
    {
      if(!zero(c[j][i]))
      {
	q = c[j][i] / c[i][i];
	for(k = i; k < n; k++) c[j][k] -= c[i][k] * q;
      }
    }
  }
    return ans;
}
int main()
{
  int n;
  while(~scanf("%d",&n))
  {
    for(int i = 0; i < n; i++)
    {
      for(int j = 0; j < n; j++) scanf("%lf",&c[i][j]);
    }
    printf("%0.2lf\n",det(n));
  }
  return 0;
}


你可能感兴趣的:(行列式求值)