Purple book The fifth chapter training 3 d-throwing cards away I

Source: Internet
Author: User

D-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 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

Like to fight the landlord you hurriedly to shuffle, you have n cards, first throw away, and then put the first one in the end, and then the first card to throw away, the remaining one card and then output you throw away this card. Would have thought of using an array analogy, similar to Joseph Ring. But GG, I tried a few groups did not, and then they were hanging in that, changed a long array suddenly found a lot of people ac, I think maybe the array is more troublesome, I need to shake clever think of some other data structure, a beat thigh this is the queue, crying dizzy in the toilet. Alas, this typical queue and brace match represents the stack itself to be very familiar with, ah, automatic hash matching AH. Kind of back to the college entrance examination feeling, but the college entrance examination I did not work so hard ah.

#include <iostream>using namespacestd;ints[ -];intN;intMain () { while(cin>>n&&N) {intk=1; intL=N;  for(intI=1; i<=n; i++) s[i]=i; if(n==1) cout<<"Discarded cards:"<<endl<<"Remaining card:1"<<Endl; Else{cout<<"Discarded cards:";  while(k<l) {cout<<S[k]; K++; if(k<l) cout<<", "; if(k==l) cout<<endl<<"Remaining Card:"<<s[k]<<Endl; s[++l]=S[k]; K++; }        }    }return 0;}
View Code

Purple book The fifth chapter training 3 d-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.