-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP130.cpp
53 lines (52 loc) · 1.19 KB
/
P130.cpp
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
#include <iostream>
int main() {
int men[100];
while(true) {
int n, k;
std::cin >> n >> k;
if(n == 0)
return 0;
// set up:
int maxI = n-1;
int i = maxI;
for(int j = 0; j < n; ++j) {
men[j] = j+1;
}
// calculate:
while(maxI > 0) {
/*
for(int j = 0; j <= maxI; ++j) {
std::cerr << " ";
if(j == i)
std::cerr << "(";
std::cerr << men[j];
if(j == i)
std::cerr << ")";
}
std::cerr << std::endl;//*/
// Kill:
int toDieI = (i + k)%(1+maxI);
// Walk to replacement:
int toReplaceI = (toDieI+1)%(1+maxI);
for(int j = 1; j < k; ++j) {
toReplaceI = (toReplaceI+1)%(1+maxI);
if(toReplaceI == toDieI)
toReplaceI = (toReplaceI+1)%(1+maxI);
}
//std::cerr << "Killing " << men[toDieI] << "@" << toDieI << " -> " << men[toReplaceI] << "@" << toReplaceI << std::endl;
// Replace dead guy:
men[toDieI] = men[toReplaceI];
// Shuffle all down:
for(int j = toReplaceI; j < maxI; ++j) {
men[j] = men[j+1];
}
--maxI;
// update i:
i = toDieI;
if(toDieI > toReplaceI)
--i;
}
// output:
std::cout << ((n+1-men[0])%n+1) << std::endl;
}
}