psbook solutions

সমস্যা ও সমাধান বই - ১ এর উপর অনুশীলন ( Practice code for Problem and Solutions Book - 1 by Mahbubul Hasan )

View the Project on GitHub

UVa 10935 - Throwing Cards Away I

Deque

Commit Time 20 Oct 2017 22:57
#include <cstdio>
#include <deque>

using namespace std;

int main() {
    int n;
    while (scanf("%d", &n) && n != 0) {
        deque<int> dq;
        for (int i = 0; i < n; i++) {
            dq.push_back(i+1);
        }
        printf("Discarded cards:");
        while (dq.size() > 1) {
            printf(" %d", dq.front());
            dq.pop_front();
            if(dq.size() != 1) printf(",");
            dq.push_back(dq.front());
            dq.pop_front();
        }
        printf("\nRemaining card: %d\n", dq.front());
    }
    return 0;
}