UVa 10935-throwing cards away I

Source: Internet
Author: User

Original question

Throwing cards away I
Given is a ordered deck ofn cards numbered 1 to n with card 1 at the top and card n at the bottom. Thefollowing operation is performed as long as there be at least, cards in Thedeck:
Throw away the top card and move Thecard 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, Remainingcard.

Input each line of input (except the last) contains a number n≤50. The lastline contains ' 0 ' and this line should is not processed.

Output for each number from the input produce, lines of output. The Firstline presents the sequence of discarded cards, the second line reports the Lastremaining card. No Line would have leading or trailing spaces. See the Samplefor 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

Title Description: There are N (n<=50) cards on the table, starting from the first one, numbered from the top down to 1-n. When there are at least two cards left, do the following: Discard the first card and put the new first card at the end of the stack. Enter each row with one n, and the output each time you discard the card and the last remaining card.

This problem can be solved in a queue, the queue is a special linear structure, only allow the queue header (head) to delete, become "out of the team", and in the queue header (tail) inserted, called "Queued."

Queue problem Solving ideas are as follows:

The first step, to delete the first number, first think about how to delete the first number of arrays, the simplest way is to move all the numbers back one, the previous number is overwritten. But if you move it every time, it's time-consuming. In this question, we introduce the integer variable head and Tail,head to record the first bit of the queue, tail the next position of the last one (here tail records the last position of the final one because when the queue is left with only one element, the first bit coincides with the last one). Whenever you delete a number, head++, waste a space, but can save a lot of time, the new addition of a number is also, the need to increase the number of the team at the end of a "tail" after the tail++ can be.

Note: the output format, when n=1, "discarded cards:" There is no space after.

Here is the code (run through)


#include <iostream>

using namespace Std;

The body of a queue, used to store content

int n;

int main ()

{

while (Cin>>n&&n)

{

int head=1;

Head is the first team, tail is the end of the team N

for (int i=1;i<=n;i++)

{

S[i]=i;

}

if (n==1)

{

cout<< "Discarded cards:" <<endl<< "Remaining card:1" <<endl;

}

Else

{

cout<< "discarded cards:";

while (Head<tail)

{

cout<<s[head];head++;

if (Head<tail)

cout<< ",";

if (Head==tail)

cout<<endl<< "Remaining card:" <<s[head]<<endl;

S[++tail]=s[head];

head++;

}

}

}

return 0;

}

UVa 10935-throwing cards away I

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.