-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeterminant_main.cpp
More file actions
80 lines (74 loc) · 1.77 KB
/
determinant_main.cpp
File metadata and controls
80 lines (74 loc) · 1.77 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
4 kyu
Matrix Determinant
https://www.codewars.com/kata/52a382ee44408cea2500074c
*/
#include <iostream>
#include <vector>
using LL = long long;
using LLMatrix = std::vector<std::vector<LL>>;
LL determinant(const LLMatrix& m);
template <typename T>
std::ostream& operator<<(std::ostream& os,
const std::vector<std::vector<T>>& m) {
os << "{";
for (size_t i = 0; i < m.size(); ++i) {
os << "{";
for (size_t j = 0; j < m[i].size(); ++j) {
os << m[i][j];
if (j + 1 < m[i].size())
os << ", ";
}
os << "}";
if (i + 1 < m.size())
os << ", ";
}
os << "}";
return os;
}
static void do_test(const LLMatrix& m, const LL expected) {
LL actual = determinant(m);
std::cout << "Matrix: " << m << std::endl
<< "Expected: " << expected << ", actual: " << actual << " -> "
<< (actual == expected ? "OK" : "FAIL") << std::endl
<< std::endl;
}
int main(void) {
{
const LLMatrix matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
do_test(matrix, 0);
}
{
const LLMatrix matrix = {{4, 6}, {3, 8}};
do_test(matrix, 14);
}
{
const LLMatrix matrix = {{2, 4, 2}, {3, 1, 1}, {1, 2, 0}};
do_test(matrix, 10);
}
{
const LLMatrix matrix = {
{2, 3, 7, 1},
{7, 1, 9, 8},
{8, 6, 1, 4},
{0, 1, 4, 2},
};
do_test(matrix, 681);
}
{
const LLMatrix matrix = {
{1, 2, 5, -7, 5, 3}, {7, -4, 6, 3, 9, 11}, {22, -16, 4, 7, 8, 1},
{-8, 0, 9, 5, 8, 1}, {77, -6, -5, 1, 23, 3}, {9, 6, -7, 3, 4, 5},
};
do_test(matrix, -3487464);
}
{
const LLMatrix matrix = {{1, 3}, {2, 5}};
do_test(matrix, -1);
}
{
const LLMatrix matrix = {{2, 5, 3}, {1, -2, -1}, {1, 3, 4}};
do_test(matrix, -20);
}
return 0;
}