Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Solution to problem Cartas by dacanizares
- #include <bits/stdc++.h>
- using namespace std;
- struct Node{
- int value;
- Node* next;
- Node* prev;
- Node(int v){
- value = v;
- next = nullptr;
- prev = nullptr;
- }
- };
- struct List{
- int count;
- Node* first;
- Node* last;
- void add(int v){
- Node* n = new Node(v);
- if(first == nullptr){
- n->next = n;
- n->prev = n;
- first = n;
- last = n;
- } else{
- n->prev = last;
- n->next = first;
- last->next = n;
- first->prev = n;
- last = n;
- }
- ++count;
- }
- void debug(){
- Node* n = first;
- for(int i = 0; i < count; ++i){
- cout << "["<< n->prev->value << "|" << n->value << "|" << n->next->value <<"] ";
- n = n->next;
- }
- cout << '\n';
- }
- void del(Node* n){
- if(count == 0)
- return;
- if(count == 1){
- first = nullptr;
- last = nullptr;
- } else {
- n->next->prev = n->prev;
- n->prev->next = n->next;
- if(n == first){
- first = n->next;
- }
- if (n == last){
- last = n->prev;
- }
- }
- delete n;
- --count;
- }
- List(){
- count = 0;
- first = nullptr;
- last = nullptr;
- }
- };
- // Some fast-coded unit test
- void tests(){
- // Inserting
- List l;
- l.add(1);
- l.add(2);
- l.add(3);
- l.debug();
- assert(l.count == 3);
- // Deleting first
- l.del(l.first);
- l.debug();
- assert(l.first->value == 2);
- assert(l.count == 2);
- assert(l.first->next == l.last);
- assert(l.first->prev == l.last);
- assert(l.last->next == l.first);
- // Deleting last
- l.del(l.last);
- l.debug();
- assert(l.last->value == 2);
- assert(l.count == 1);
- assert(l.first->next == l.last);
- assert(l.first->prev == l.last);
- assert(l.last->next == l.first);
- // Adding after deleting
- l.add(6);
- l.debug();
- assert(l.last->value == 6);
- assert(l.count == 2);
- assert(l.first->next == l.last);
- assert(l.first->prev == l.last);
- assert(l.last->next == l.first);
- // Deleting all
- l.del(l.first);
- l.del(l.first);
- assert(l.first == nullptr);
- // Adding
- l.add(1);
- l.debug();
- assert(l.last->value == 1);
- assert(l.count == 1);
- assert(l.first->next == l.last);
- assert(l.first->prev == l.last);
- assert(l.last->next == l.first);
- }
- // Prints the answer
- void print(List *l){
- Node* cur = l->first;
- for(int i = 0; i < l->count; ++i){
- switch(cur->value){
- case 1: cout << 'A'; break;
- case 10: cout << 'J'; break;
- case 11: cout << 'Q'; break;
- case 12: cout << 'K'; break;
- default: cout << cur->value; break;
- }
- cout << " ";
- cur = cur->next;
- }
- }
- int main(){
- //tests();
- int n, k, t;
- while(cin >> n >> k >> t){
- List l;
- for(int i = 0; i < n; ++i){
- l.add(k);
- k++;
- if(k == 13) k = 1;
- }
- Node* top = l.first;
- Node* bot = l.last;
- for(int i = 0; i < (n + 1) / 2; ++i){
- if(t == 1){
- swap(top->value, top->next->value);
- top = top->next->next;
- } else {
- swap(top->value, bot->value);
- top = top->next;
- bot = bot->prev;
- }
- }
- List l2;
- Node* cur = l.first;
- Node* next;
- for(int i = 0; i < n; ++i){
- next = cur->next;
- if(t == 1){
- if((i+1) % 2 == 0 && cur->value % 2 == 0){
- l2.add(cur->value);
- l.del(cur);
- }
- } else {
- if((i+1) % 2 != 0 && cur->value % 2 != 0){
- l2.add(cur->value);
- l.del(cur);
- }
- }
- cur = next;
- }
- print(&l);
- print(&l2);
- cout << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement