forked from cp-algorithms/cp-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_josephus.cpp
More file actions
47 lines (41 loc) · 910 Bytes
/
test_josephus.cpp
File metadata and controls
47 lines (41 loc) · 910 Bytes
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
#include <cassert>
#include <vector>
using namespace std;
namespace Fast0 {
#include "josephus_fast0.h"
}
namespace Rec {
#include "josephus_rec.h"
}
namespace Iter {
#include "josephus_iter.h"
}
int brute_force_josephus(int n, int k) {
vector<bool> v(n, true);
int rem = n;
int idx = -1;
while (rem > 0) {
int todo = k;
while (todo > 0) {
idx = (idx + 1) % n;
if (v[idx])
todo--;
}
v[idx] = false;
rem--;
}
return idx + 1;
}
void test_josephus() {
for (int n = 1; n <= 50; n++) {
for (int k = 1; k <= 50; k++) {
int expected = brute_force_josephus(n, k);
assert(expected == Fast0::josephus(n, k) + 1);
assert(expected == Rec::josephus(n, k));
assert(expected == Iter::josephus(n, k));
}
}
}
int main() {
test_josephus();
}