include iostream include vector include cmath using namespace std void

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
void Getminor(vector <vector<double> > a, vector <vector<double> > &b, int x, int y, int size)
{
int xCount = 0, yCount = 0;
for (int i = 0; i < size; i++)
{
if (i != x)
{
yCount = 0;
for (int j = 0; j < size; j++)
{
if (j != y)
{
b[xCount][yCount] = a[i][j];
yCount++;
}
}
xCount++;
}
}
}
double FindDeterminant(vector <vector<double> > a, int size)
{
// останавливаем рекурсию, если матрица
// состоит из одного элемента
if (size == 1)
{
return a[0][0];
}
else
{
double det = 0.0;
vector <vector<double> > minor(size - 1, vector <double> (size -1));
for (int i = 0; i < size; i++)
{
Getminor(a, minor, 0, i, size);
// Рекурсия
det += (((i % 2) == 0) ? 1 : -1) * a[0][i] * FindDeterminant(minor, size - 1);
}
return det;
}
}
int main()
{
freopen("input.txt", "r", stdin);
int n;
cin >> n;
vector <vector<double> > a(n, vector <double> (n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >>a[i][j];
}
}
cout << "n:" << n << endl;
cout << "matr a:" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cout << a[i][j] <<" ";
cout << endl;
}
cout << endl;
// находим определитель матрицы
double determinant = FindDeterminant(a, n);
cout << "Determinant matr a:" << endl << determinant;
cout << endl;
}