Advertisement
dacanizares

Solution to problem Cartas

Oct 24th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. // Solution to problem Cartas by dacanizares
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. struct Node{
  7.     int value;
  8.     Node* next;
  9.     Node* prev;
  10.  
  11.     Node(int v){
  12.         value = v;
  13.         next = nullptr;
  14.         prev = nullptr;
  15.     }
  16. };
  17.  
  18. struct List{
  19.     int count;
  20.     Node* first;
  21.     Node* last;
  22.  
  23.     void add(int v){
  24.         Node* n = new Node(v);
  25.         if(first == nullptr){
  26.             n->next = n;
  27.             n->prev = n;
  28.  
  29.             first = n;
  30.             last = n;
  31.         } else{
  32.             n->prev = last;
  33.             n->next = first;
  34.  
  35.             last->next = n;
  36.             first->prev = n;
  37.  
  38.             last = n;
  39.         }
  40.  
  41.         ++count;
  42.     }
  43.  
  44.     void debug(){
  45.         Node* n = first;
  46.         for(int i = 0; i < count; ++i){
  47.             cout << "["<< n->prev->value << "|" << n->value << "|" << n->next->value <<"] ";
  48.             n = n->next;
  49.         }
  50.         cout << '\n';
  51.     }
  52.  
  53.     void del(Node* n){
  54.         if(count == 0)
  55.             return;
  56.  
  57.         if(count == 1){
  58.             first = nullptr;
  59.             last = nullptr;
  60.         } else {
  61.             n->next->prev = n->prev;
  62.             n->prev->next = n->next;
  63.  
  64.             if(n == first){
  65.                 first = n->next;
  66.             }
  67.             if (n == last){
  68.                 last = n->prev;
  69.             }
  70.         }
  71.  
  72.         delete n;
  73.         --count;
  74.     }
  75.  
  76.     List(){
  77.         count = 0;
  78.         first = nullptr;
  79.         last = nullptr;
  80.     }
  81. };
  82.  
  83. // Some fast-coded unit test
  84. void tests(){
  85.     // Inserting
  86.     List l;
  87.     l.add(1);
  88.     l.add(2);
  89.     l.add(3);
  90.  
  91.     l.debug();
  92.     assert(l.count == 3);
  93.  
  94.     // Deleting first
  95.     l.del(l.first);
  96.     l.debug();
  97.     assert(l.first->value == 2);
  98.     assert(l.count == 2);
  99.     assert(l.first->next == l.last);
  100.     assert(l.first->prev == l.last);
  101.     assert(l.last->next == l.first);
  102.  
  103.     // Deleting last
  104.     l.del(l.last);
  105.     l.debug();
  106.     assert(l.last->value == 2);
  107.     assert(l.count == 1);
  108.     assert(l.first->next == l.last);
  109.     assert(l.first->prev == l.last);
  110.     assert(l.last->next == l.first);
  111.  
  112.     // Adding after deleting
  113.     l.add(6);
  114.     l.debug();
  115.     assert(l.last->value == 6);
  116.     assert(l.count == 2);
  117.     assert(l.first->next == l.last);
  118.     assert(l.first->prev == l.last);
  119.     assert(l.last->next == l.first);
  120.  
  121.     // Deleting all
  122.     l.del(l.first);
  123.     l.del(l.first);
  124.     assert(l.first == nullptr);
  125.  
  126.     // Adding
  127.     l.add(1);
  128.     l.debug();
  129.     assert(l.last->value == 1);
  130.     assert(l.count == 1);
  131.     assert(l.first->next == l.last);
  132.     assert(l.first->prev == l.last);
  133.     assert(l.last->next == l.first);
  134. }
  135.  
  136. // Prints the answer
  137. void print(List *l){
  138.     Node* cur = l->first;
  139.     for(int i = 0; i < l->count; ++i){
  140.         switch(cur->value){
  141.             case 1: cout << 'A'; break;
  142.             case 10: cout << 'J'; break;
  143.             case 11: cout << 'Q'; break;
  144.             case 12: cout << 'K'; break;
  145.             default: cout << cur->value; break;
  146.  
  147.         }
  148.  
  149.         cout << " ";
  150.  
  151.         cur = cur->next;
  152.     }
  153. }
  154.  
  155. int main(){
  156.     //tests();
  157.  
  158.     int n, k, t;
  159.     while(cin >> n >> k >> t){
  160.         List l;
  161.         for(int i = 0; i < n; ++i){
  162.             l.add(k);
  163.  
  164.             k++;
  165.             if(k == 13) k = 1;
  166.         }
  167.  
  168.         Node* top = l.first;
  169.         Node* bot = l.last;
  170.         for(int i = 0; i < (n + 1) / 2; ++i){
  171.             if(t == 1){
  172.                 swap(top->value, top->next->value);
  173.                 top = top->next->next;
  174.             } else {
  175.                 swap(top->value, bot->value);
  176.                 top = top->next;
  177.                 bot = bot->prev;
  178.             }
  179.         }
  180.  
  181.         List l2;
  182.         Node* cur = l.first;
  183.         Node* next;
  184.         for(int i = 0; i < n; ++i){
  185.             next = cur->next;
  186.             if(t == 1){
  187.                 if((i+1) % 2 == 0 && cur->value % 2 == 0){
  188.                     l2.add(cur->value);
  189.                     l.del(cur);
  190.                 }
  191.             } else {
  192.                 if((i+1) % 2 != 0 && cur->value % 2 != 0){
  193.                     l2.add(cur->value);
  194.                     l.del(cur);
  195.                 }
  196.             }
  197.             cur = next;
  198.         }
  199.  
  200.         print(&l);
  201.         print(&l2);
  202.         cout << '\n';
  203.     }
  204.     return 0;
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement