Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

programming in c data structure (card game)

+6 votes
asked Jul 13, 2021 by paulo magalhaes (1,480 points)
Given a pile of n cards numbered from 1 to n with card 1 at the top and card n at the bottom. The following operation is performed while you have 2 or more cards in the pile.

Discard the top card and move the next card (the one on top) to the bottom of the pile.

Your task is to find the sequence of discarded cards and the last remaining card.

Each input line (except the last) contains a number n ≤ 50. The last line contains 0 and must not be processed. Each input number produces two lines of output. The first row shows the sequence of discarded cards and the second row shows the remaining card.
Input

The input consists of an indeterminate number of lines each containing a value from 1 to 50. The last line contains the value 0.
Exit

For each test case, print two lines. The first line presents the sequence of discarded cards, each one separated by a comma and a space. The second line shows the number of the remaining card. No lines have extra spaces at the beginning or end. See example to check the expected format.
Input Example Output Example

7
19
10
6
0


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

2 Answers

0 votes
answered Jul 13, 2021 by Peter Minarik (86,040 points)
edited Jul 15, 2021 by Peter Minarik

LOL, I had to read this like 10 times until, I understood what needs to be done here. :)

So you have n number of cards in the deck. You also have a discard pile, which is empty when you start.

What you need to do is the following:

  1. Do we have at least 2 cards in the deck? No, then STOP.
  2. Take the top card from the deck and put it into the discard pile. That card is gone. Forever.
  3. Take the new top card from the deck and put it on the bottom of the deck. This card is still in the deck, just from being the first, now it is moved to be the last.
  4. Go to #1 and repeat the steps.

Now, that the algorithm is clear, I'm sure you can do the implementation. It shouldn't be too hard.

Good luck!

0 votes
answered Jul 13, 2021 by GIRISH SAI G. J (140 points)
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
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...