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