Throwing cards away I, throwingcardsaway
Throwing cards away I
Given is an 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 med as long as there are at least two cards in the deck:
Throw away the top card and move the card that is 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.
InputEach line of input (iterator t the last) contains a number n ≤ 50. The last line contains '0' and this line shoshould not be processed.
OutputFor each number from the input produce two lines of output. the first line presents the sequence of discarded cards, the second line reports the last remaining card. no line will 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 this question refers to taking out a card and placing the second card at the end until the last card is Remaining. Output the cards in turn and output the remaining card Code as follows: (for beginners, refer to the method in the book. Hope you can understand)
# Include <iostream>
# Include <string. h>
Using namespace std;
Int main ()
{
Int n;
While (cin> n & n)
{
Int a [100], B [100], c [1];
Int I, j = 0, head, tail;
Head = 0; // mark the head to the beginning of the queue
Tail = n; // mark the tail to the last digit of the queue, because you need to leave a position to the number at the beginning.
For (I = 0; I <n; I ++)
A [I] = I + 1; // 1 2 3 4 5 6 7
If (n = 1)
{
Cout <"Discarded cards:" <endl;
Cout <"Remaining card: 1" <endl;
}
Else
{
While (head + 1 <tail) // The Judgment condition here indicates that when array a has not reached a number, it will loop down.
{
B [j ++] = a [head]; // assign the first number to array B and increase j from 0.
Head ++; // The head moves one digit backward, removing the "first number" 2 3 4 5 6 7
A [tail] = a [head]; // move the second number to the last 2 3 4 5 6 7 2 of the queue
C [0] = a [tail]; // each time the last number is assigned to the array c with a length of 1. When the last number is left, c [0] is the remaining number
Tail ++; // tail moves one digit backward to reserve a position for the next move to the end of the team
Head ++; // because the first queue has been moved to the end of the team, the head has been moved to the back of a 3 4 5 6 7 2
}
Cout <"Discarded cards :";
For (int k = 0; k <= J-2; k ++) // because j ++. When the while clause is exceeded, j adds 1 more, so 1 more
Cout <B [k] <",";
Cout <B [J-1] <endl; // same as the previous
Cout <"Remaining card :";
Cout <c [0] <endl;
}
}
}
Pay special attention to whether there are too few spaces and symbols in the output. I have suffered a long time .....