Algorithms Exercises --- 5-3 card game (UVa10935)

A: Title

Given n cards, the 1- sequence number n, and then throw out a card, a card put the final out, this operation is repeated until only one card. 

Card serial number and a request to throw away the last remaining cards.

(A) Sample Input

7 // cards numbered 1-7 from 
19 // cards numbered from 1 to 19 
10 
6 
0

(B) Sample Output

Discarded cards: 1, 3, 5, 7, 4, 2
Remaining card:6
Discarded cards: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 4, 8, 12, 16, 2, 10, 18, 14
Remaining card:6
Discarded cards: 1, 3, 5, 7, 9, 2, 6, 10, 8
Remaining card:4
Discarded cards: 1, 3, 5, 2, 6
Remaining card:4

Two: code implementation

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <queue>usingnamespace std;#define MAX_N 15int main103()
{
    freopen("data5_3_h.in", "r", stdin);
    freopen("data5_3_h.out", "w", stdout);int num,first_ele;
    while (cin>>num&&num!=0)
    { queue<int> card;// simulated using a queue
        bool

 





    
        flag = false;
        for (int i = 1; i <= num; i++)
            card.push(i);
        cout << "Discarded cards: ";
        while (card.size()!=1)
        {
            if (!flag)
            {
                cout << card.front();
                if (card.size() != 2)
                    cout << ", ";
            }
            else
                card.push(card.front());
            card.pop();
            flag = !flag;
        }
        cout << "\nRemaining card:" << card.front() << endl;
    }

    freopen("CON", "r", stdin);
    freopen("CON", "w", stdout);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/ssyfj/p/11539576.html