Advertisement
ikReza

Throwing Cards away

Jan 27th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <iterator>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     list <int> li;
  10.     list <int> li2;
  11.     list <int> :: iterator it;
  12.  
  13.     while(true)
  14.     {
  15.         int num;
  16.         cin >> num;
  17.         if(num == 0)
  18.             break;
  19.         else
  20.         {
  21.             for(int i = 1; i <= num; i++)
  22.                 li.push_back(i);
  23.  
  24.             int liCount = li.size();
  25.             int take, give;
  26.             while(liCount > 1)
  27.             {
  28.                 take = li.front();
  29.                 li.pop_front();
  30.                 li2.push_back(take);
  31.                 give = li.front();
  32.                 li.pop_front();
  33.                 li.push_back(give);
  34.                 liCount--;
  35.             }
  36.  
  37.             cout << "Discarded cards: ";
  38.             for(it = li2.begin(); it != li2.end(); it++)
  39.             {
  40.                 cout << *it;
  41.                 if(*it != li2.back())
  42.                     cout << ", ";
  43.             }
  44.             cout << endl;
  45.  
  46.             it = li.begin();
  47.             cout << "Remaining card: " << *it << endl;
  48.         }
  49.         li.clear();
  50.         li2.clear();
  51.     }
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement