50214. Determinant
難度:3/5
看到WA就先開long long,然後遞迴內每層都改的都放近function內。
Second Try: 3/5 Used Time: 12:47 Used Time: 8:451
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#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
void print_arr(int64_t arr[8][8], int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf("%lld%c", arr[i][j], (j == n - 1) ? '\n' : ' ');
}
}
}
int64_t cal_det(int64_t arr[8][8], int n){
int64_t temp[8][8];
print_arr(arr, n);
if(n == 1) return arr[0][0];
int64_t ans = 0;
int tx = 0, ty = 0;
for(int k = 0; k < n; k++){
tx = 0;
for(int i = 1; i < n; i++){
ty = 0;
for(int j = 0; j < n; j++){
if(j == k) continue;
temp[tx][ty] = arr[i][j];
ty++;
}
tx++;
}
int64_t s = arr[0][k] * cal_det(temp, n - 1);
if(k % 2 == 0) ans += s;
else ans -= s;
}
printf("determinant=%lld\n", ans);
return ans;
}
signed main(){
int n;
int64_t arr[8][8];
scanf("%d", &n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%lld", &arr[i][j]);
}
}
cal_det(arr, n);
}