[Queue] Apsara stack games

Source: Internet
Author: User
Description

Jack is a famous flying Pan expert in a town in the United States. When throwing a Frisbee, he had a habit of starting from the first Frisbee (that is, the top Frisbee) in a stack, numbered 1, 2 ,..., n. When there are at least two Frisbee, Jack usually rolls out a Frisbee and puts the first new one at the end of all the Frisbee. Input n to output the discarded Frisbee and the remaining Frisbee.

InputThe first act is an integer T (0 <t <20), indicating the number of test cases. Each line in the following T contains an integer N (0 <n <40), which is the number of flying saucer in a test case. Output

Output a single row for each test case. In this row, the numbers of the fl and the remaining fl are output in sequence. Each fl is followed by a space.

Sample Input
274
Sample output
1 3 5 7 4 2 61 3 2 4
Problem Source: course exercise questions

Solution:

Use a queue to simulate the actual situation

Implementation Code:

# Include <iostream> # include <queue> Using STD: cout; Using STD: CIN; Using STD: Endl; Using STD: queue; int main () {int t, n, TMP; queue <int> q; CIN> T; For (; t> 0; t --) {// t test cases: CIN> N; For (INT I = 0; I <n; I ++) {// initialize the stack and number q in sequence. push (I + 1) ;}for (INT I = 0; q. size ()> 2;) {cout <q. front () <"; // throw the Frisbee and the Q. pop (); q. push (Q. front (); // the next one is placed at the end of the Q. pop ();} If (Q. size () = 2) {// when only two frisbee are left, cout is output in sequence <q. front () <"; q. pop ();} cout <q. front () <"; q. pop (); cout <Endl ;}}

(I have referenced other online materials more or less in this blog, but I have forgotten my reference for a long time. I would like to thank them !)

[Queue] Apsara stack games

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.