这个问题搞定了,用的是代数余子式的思路.
有个重载函数,主要就是递归,把高阶递归到二阶的来算.
#include <iostream>
using namespace std;
void main()
{
float **getD(int &size);
float sum(float **s, int size, int n);
float sum(float **s, float *f, int size, int n);
int size;
float **s;
s = getD(size);
cout <<"The sum of D is:"<<sum(s, size, size-1)<<endl;
}
float **getD(int &size)
{
int i,j;
float **s;
cout << "Please input the size of D:";
cin >> size;
s = new float *[size];
for (i = 0; i <= size; i++)
{
s[i] = new float [size];
if (s[i] == NULL)
{
cout <<"Fail";
exit (1);
}
}
--size;
cout <<"Please input the D:";
for (i = 0; i <= size; i++)
for (j = 0; j <= size; j++)
cin >>s[i][j];
return s;
}
float sum(float **s, float *f, int size, int n)
{
int i;
float d=0;
if (n)
{
for (i = 0; i <= size; i++)
if (f[i])
{
f[i] = 0;
if ((i+size-n-1) % 2)
d += s[i][size - n - 1] * sum(s, f, size, n-1) * (-1);
else
d += s[i][size - n - 1] * sum(s, f, size, n-1);
f[i] = 1;
}
}
else
{
for (i = 0; i <= size; i++)
if (f[i])
for (int j = 0; j <= size; j++)
if (f[j]&&(j != i))
return (s[i][size - 1] * s[j][size] - s[i][size] * s[j][size -1]);
}
return d;
}
float sum(float **s, int size, int n)
{
int i;
float *f;
f = new float [size];
if ((s == NULL) || (f ==NULL))
{
cout <<"Fail";
exit (1);
}
for (i = 0; i <= size; i++)
f[i] = 1;
return sum(s, f, size, n);
}