-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcards.cpp
86 lines (73 loc) · 1.66 KB
/
cards.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
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
81
82
83
84
85
86
#include <cstdlib>
#include <deque>
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::string;
int CARDS_TOTAL;
using Deck = std::deque<int>;
void newstack(Deck &deck) {
for(int i = 0; i < CARDS_TOTAL / 2; ++i) {
int temp = deck[i];
deck[i] = deck[CARDS_TOTAL - 1 - i];
deck[CARDS_TOTAL - 1 - i] = temp;
}
}
void cut(Deck &deck, int number) {
if(number > 0) {
while(number --> 0) {
int first = deck[0];
deck.push_back(first);
deck.pop_front();
}
} else {
while(number ++< 0) {
int last = deck[CARDS_TOTAL-1];
deck.push_front(last);
deck.pop_back();
}
}
}
void increment(Deck &deck, int number) {
Deck copy = deck;
int position = 0;
while(copy.size() > 0) {
deck[position] = copy[0];
copy.pop_front();
position = (position + number) % CARDS_TOTAL;
}
}
int main() {
string line;
std::getline(cin, line);
CARDS_TOTAL = std::stoi(line);
Deck deck;
for(int i = 0; i < CARDS_TOTAL; ++i) {
deck.push_back(i);
}
while(std::getline(cin, line)) {
if(line.find("deal into new stack") != string::npos) {
newstack(deck);
} else if(line.find("deal with increment") != string::npos) {
increment(deck, std::stoi(line.substr(19)));
} else if(line.find("cut") != string::npos) {
cut(deck, std::stoi(line.substr(3)));
} else {
cout << "Unknown operation: \"" << line << "\"\n";
exit(EXIT_FAILURE);
}
}
if(CARDS_TOTAL <= 100) {
for(int i = 0; i < CARDS_TOTAL; ++i) cout << deck[i] << " ";
cout << "\n";
} else {
cout << "deck[2019]: " << deck[2019] << "\n";
for(int i = 0; i < CARDS_TOTAL; ++i) {
if(deck[i] == 2019) {
cout << "deck[" << i << "]: 2019\n";
break;
}
}
}
}