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… 样例输入
-
245
样例输出
-
2 1 4 33 1 4 5 2
分析:
题意就是第几次就从上面拿几张牌放到下面,然后把当前最上面的牌拿出,直到纸牌全部取出,需要注意的是:题中要输出的是每张牌输出的时间,而不是牌的输出顺序,所以需要另外一个数组存储输出时间;
#include<stdio.h>
#include<queue>
using namespace std;
int main()
{
int k,m,n,a[15],b[15],i,j;
scanf("%d",&k);
while(k--)
{
queue<int>q;
scanf("%d",&m);
for(i=1; i<=m; i++)
q.push(i);//先把所有的数据都放入队列
for(i=1; i<=m; i++)
{
n=i;
while(n--)//把i张牌放在下边,重新开始洗牌
{
int t=q.front();
q.pop();
q.push(t);
}
a[i]=q.front();//把当前最上面的一张存入数组
q.pop();
}
for(i=1; i<=m; i++)//把a数组每个数据输出时间存入b数组
b[a[i]]=i;
for(i=1;i<m;i++)//需要输出的每个数据输出的时间
printf("%d ",b[i]);
printf("%d\n",b[m]);
}
return 0;
}