10935 Throwing cards away
I Given is a ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there be at least, cards in the Deck:throw away the top card and move The card that's now on the top of the deck to the bottom of the deck. Your task is to find the sequence of discarded cards and the last, remaining card.
Input
Each line of input (except) contains a number n≤50. The last line contains ' 0 ' and this line should is processed.
Output
For each number from the input produce, lines of output. The first line presents the sequence of discarded cards, the second line reports the last remaining card. No Line would have leading or trailing spaces. See the sample for the expected format.
Sample Input
7
19
10
6
0
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
Problem Solving Ideas: We first enter an X to determine the number of cards we need to give and the numbers above our cards. We then use Push_back () to store the number of each of our inputs in a set container.
(Create a set container, we need to use the <map> this header file) we have more than 1 o'clock in our cards to throw the operation. Use the front () function to output the first number, then remove the pop () function from the first number, remove the first number with the front () function, and use the push () function to insert it behind the container and delete the number with the pop function. Finally, we're going to erase the rest of the data from the container. (We should pay attention to our output form)
#include <iostream>#include<queue>using namespaceStd;queue<int>v;intMain () {intx; while(cin>>x&&x) { for(intI=0; i<x;i++) V.push (i+1); cout<<"Discarded cards:"; while(V.size () >1) { if(V.size () >2) cout<<" "<<v.front () <<","; Elsecout<<" "<<V.front (); V.pop (); V.push (V.front ()); V.pop (); } cout<<Endl; cout<<"Remaining Card:"<<" "<<v.front () <<Endl; V.pop (); } return 0;}
program code:
ACM Card Game