Program Design Competition for the sixth college student in Henan -- Card Trick, Henan -- card
Card Trick
Time Limit: 2 Seconds Memory Limit: 64 MB
Description
The magician shuffles a small pack of cards, holds it face down and performs the following procedure:
1. The top card is moved to the bottom of the pack. The new top card is dealt face up onto the table. It is the Ace of Spades.
2. Two cards are moved one at a time from the top to the bottom. The next card is dealt face up onto the table. It is the Two of Spades.
3. Three cards are moved one at a time...
4. This goes on until the nth and last card turns out to be the n of Spades.
This impressive trick works if the magician knows how to arrange the cards beforehand (and knows how to give a false shuffle ). your program has to determine the initial order of the cards for a given number of cards, 1 ≤ n ≤ 13.
Input
On the first line of the input is a single positive integer k, telling the number of test cases to follow. 1 ≤ k ≤ 10 Each case consists of one line containing the integer n. 1 ≤ n ≤ 13
Output
For each test case, output a line with the correct permutation of the values 1 to n, space separated. The first number showing the top card of the pack, etc...
Sample Input
245
Sample Output
2 1 4 33 1 4 5 2
Source
Henan sixth College Student Program Design Competition
In the past, the question of the provincial competition was quite watery !!
A stack of cards, for example, 2, 1, 4, and 3. Put the top 1 card (that is, 2) to the end. Then, the order of cards is (1, 4, 3, 2) opening the first one is 1 (A), taking A away, card Order (4 3 2), and putting the top two cards (that is, 4 3) put it to the end (2 4 3), open the first one is 2, take 2 away (4 3) and then 4 3 for three exchanges, open the first card is 3.
The second one is: Put the top 1 card (3) to the bottom (1 4 5 2 3), open the first card, and remove 1 (4 5 2 3) put the above two sheets to the end (2, 3, 4, 5), open the first one, and remove 2 (3, 4, 5), put the above three sheets to the end, or (3, 4, 5) open the first one is 3, remove 3 (4 5) put the above four to the end (can be recycled) (4 5) open the first one is 4 ..
Idea: Write a queue and put the queue header at the end of the queue !! Just loop several times !! Read the Code if you don't understand it !!
AC code:
#include <cstdio>#include <iostream> #include <cstring>#include <cstdlib>#include <cmath>#include <stack>#include <queue>#include <algorithm>#include <string>using namespace std;int main(){queue<int> q;int k, n, a[20];scanf("%d", &k);while(k--){scanf("%d", &n);q.push(n);for(int i=n-1; i>=1; i--){q.push(i);for(int j=0; j<i; j++){q.push(q.front());q.pop();}}for(int i=0; i<n; i++){a[i] = q.front(); q.pop(); }for(int i=n-1; i>0; i--){printf("%d ", a[i]);}printf("%d\n", a[0]);}return 0;}