-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBEE-1110.cpp
More file actions
67 lines (52 loc) · 1.48 KB
/
Copy pathBEE-1110.cpp
File metadata and controls
67 lines (52 loc) · 1.48 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
#include <iostream>
int main()
{
int n {};
while (std::cin >> n && n != 0) {
if (n == 1) {
std::cout << "Discarded cards:\nRemaining card: 1\n";
continue;
}
std::cout << "Discarded cards: ";
int last_even {};
int last_odd {};
bool is_even = n % 2 == 0;
if (is_even) {
last_even = n;
last_odd = n - 1;
} else {
last_even = n - 1;
last_odd = n;
}
for (int i = 1; i < last_odd; i += 2) {
std::cout << i << ", ";
}
std::cout << last_odd;
int starts_at { 2 };
int ends_at { last_even };
int curr_num { 2 };
int jump_factor { 2 };
bool discard_next = is_even;
while (starts_at != ends_at) {
if (curr_num > ends_at) {
curr_num = starts_at;
jump_factor *= 2;
}
if (discard_next) {
if (curr_num == starts_at) {
starts_at += jump_factor;
} else if (curr_num == ends_at) {
ends_at -= jump_factor;
}
std::cout << ", " << curr_num;
discard_next = false;
} else {
discard_next = true;
}
curr_num += jump_factor;
}
std::cout << "\n";
std::cout << "Remaining card: " << starts_at << "\n";
}
return 0;
}