E Card Trick(队列)
Card Trick
时间限制:1000 ms | 内存限制:65535 KB
难度:3
- 描述
-
The magician shuffles a small pack of cards, holds it face down and performs the following procedure:
- 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.
- 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.
- Three cards are moved one at a time…
- 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.
- 输入
- 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
- 输出
- 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…
- 样例输入
-
2 4 5
- 样例输出
-
2 1 4 3
-
3 1 4 5 2
大致题意:
一沓牌,把最上面的一张放在最下面,此时最上面的牌是黑桃A
接着把上面的二张牌放在最下面,此时最上面的牌是黑桃2
接着把上面的三牌放在最下面,此时最上面的牌是黑桃3
........
接着把上面的n张牌放在最下面,此时最上面的牌是黑桃n
求原始序列
解题思路:
用队列模拟过程,首先存入队列的为数组下标,然后进行一次操作,取队首元素,此下标数组存的为操作的编号
我的代码:
#include<bits/stdc++.h> using namespace std; int main() { int num; scanf("%d",&num); while(num--) { int a[15]= {0},n,i,j,t; scanf("%d",&n); queue<int>q; for(i=1; i<=n; i++)//数组下标进队列 q.push(i); int ok=1; for(i=1; i<=n; i++) { j=i; while(j--)//把j个牌放在底部 { t=q.front(); q.pop(); q.push(t); } a[q.front()]=ok;//抽取最上面的一个为上面显示的是ok q.pop(); ok++; } for(i=1;i<n;i++) printf("%d ",a[i]); printf("%d\n",a[n]); } return 0; }