第一次发代码,来试试水~~
#include<iostream>
#include<vector>
using namespace std;
using Vector = vector<vector<int>>;
class D
{
protected:
int num;
int level;
Vector arr;
public:
D():num(1), level(1) {}
int(*x)[5] = new int[120][5];
void init()
{
cout << "请输入行列式长度:";
cin >> num;
arr.resize(num);
system("cls");
int temp;
for (int i = 0; i < num; i++)
{
for (int j = 0; j < num; j++)
{
cin >> temp;
arr[i].push_back(temp);
}
}
}
int getD()
{
int result = 0;
for (int i = num; i > 1; i--)
{
level *= i;
}
int* y = new int[num]();
vector<int> m;
for (int i = 0; i < num; i++)
{
*(y + i) = i;
m.push_back(i);
}
getX(m, 0, 0, level);
for (int i = 0; i < level; i++)
{
result += multip(arr, x[i]);
}
return result;
}
int ab(int a[])//判断正负
{
int data = 0;
for (int i = 0; i < num; i++)
{
for (int j = i + 1; j < num; j++)
{
if (a[i] > a[j])
data++;
}
}
if (data % 2 == 0)
return 1;
else
return -1;
}
int multip(Vector vec, int a[])
{
int data = 1;
for (int i = 0; i < num; i++)
{
data *= vec[i][a[i]];
}
data *= ab(a);
return data;
}
void getX(std::vector<int> vec, int a, int b, int size)//求列标的所有排列组合
{
int size_t = 1;
if (num - b != 0)
size_t = size / (num - b);
for (int i = 0; i < num - b; i++)
{
vector<int> temp = vec;
for (int j = 0; j < size_t; j++)
{
x[a + j][b] = temp[i];
}
temp.erase(temp.begin() + i);
getX(temp, a, b + 1, size_t);
a += size_t;
}
}
};
int main()
{
D d;
d.init();
cout << "D=" << d.getD() << endl;
system("pause");
return 0;
}