-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeterminant(Gauss).cpp
More file actions
63 lines (48 loc) · 1.07 KB
/
Determinant(Gauss).cpp
File metadata and controls
63 lines (48 loc) · 1.07 KB
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
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
using namespace std;
using ld = long double;
ifstream fin("determinant.in");
ofstream fout("determinant.out");
const int kN = 10;
const ld EPS = 1e-10;
ld a[1 + kN][1 + kN];
int main() {
int n;
fin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
fin >> a[i][j];
}
}
ld det = 1;
for (int p = 1; p <= n; ++p) {
int pivot = p;
for (int i = p; i <= n; ++i) {
if (abs(a[pivot][p]) < abs(a[i][p])) {
pivot = i;
}
}
if (abs(a[pivot][p]) < EPS) {
det = 0;
break;
}
if (p != pivot) {
det = -det;
}
for (int j = p; j <= n; ++j) {
swap(a[p][j], a[pivot][j]);
}
det *= a[p][p];
for (int i = p + 1; i <= n; ++i) {
ld f = a[i][p] / a[p][p];
for (int j = p; j <= n; ++j) {
a[i][j] -= f * a[p][j];
}
}
}
fout << fixed << setprecision(12) << det << '\n';
fin.close();
fout.close();
return 0;
}