c++使用vector求矩阵的A(行列式)
#include <vector>
#include <iostream>
using namespace std;
bool Iseven(int num)
{
return ((num & 1) == 0);
}
bool PowerIsPosition(vector<double>& vec)
{
int count = 0;
for (int i = 0; i < (int)vec.size(); i++)
{
for (int j = i + 1; j < (int)vec.size(); j++)
{
if (vec[i] > vec[j])
{
count += 1;
}
}
}
return (Iseven(count));
}
void swap(vector<double>& vec, int i, int j)
{
int temp = vec[i];
vec[i] = vec[j];
vec[j] = temp;
}
void Perm(vector<double>& vec, vector<vector<double> >& vec_seq, int current_index)
{
if (current_index == ((int)vec.size() - 1))
{
vec_seq.push_back(vec);
}
else
{
for (int i = current_index; i < (int)vec.size(); i++)
{
swap(vec, i, current_index);
Perm(vec, vec_seq, current_index + 1);
swap(vec, i, current_index);
}
}
}
vector<double> inivec(int n)
{
vector<double> vec;
for (int i = 0; i < n; i++)
vec.push_back(i);
return vec;
}
double calculate(vector<vector<double>> array)
{
int n = array.size();
vector<vector<double> > vec_que;
vector<double> vec = inivec(n);
vector<double> vec_elem;
Perm(vec, vec_que, 0);
double result = 0;
for (int i = 0; i < (int)vec_que.size(); i++)
{
vec_elem = vec_que[i];
double mi = PowerIsPosition(vec_elem) ? 1 : (-1);
double temp = mi;
int row = 0;
for (int j = 0; j < (int)vec_elem.size(); j++)
{
int col = vec_elem[j];
temp *= array[row++][col];
}
result += temp;
}
return result;
}
int main() {
vector<vector<double>> nums1 = {
{1, 2, 3},
{4, 15, 6},
{7, 8, 8}
};
double w = calculate(nums1);
cout << w << endl;
}