-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathp8.c
74 lines (52 loc) · 1.23 KB
/
p8.c
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
#include <stdlib.h>
#include <stdio.h>
int determinant(const int* a, int n) {
if (n <= 0) {
return 0;
}
if (n == 1) {
return a[0];
}
if (n == 2) {
int x = a[0];
int y = a[1];
int z = a[2];
int w = a[3];
return x * w - y * z;
}
const int m = n - 1;
// dezvoltare dupa prima linie
int semn = 1, det = 0;
for (int j = 0; j < n; ++j) {
int* minor = (int*)malloc(m * m * sizeof(int));
int k = 0;
for (int x = 1; x < n; ++x) {
for (int y = 0; y < n; ++y) {
if (j == y) {
continue;
}
minor[k++] = a[x * n + y];
}
}
int det_minor = determinant(minor, m);
free(minor);
det += semn * a[j] * det_minor;
semn *= -1;
}
return det;
}
int main() {
FILE* fin = fopen("p8.in", "r");
int n;
fscanf(fin, "%d", &n);
int* a = (int*)malloc(sizeof(int) * n * n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
fscanf(fin, "%d", &a[i * n + j]);
}
}
fclose(fin);
int det = determinant(a, n);
printf("%d\n", det);
free(a);
}