题目链接:Card Trick
Card Trick
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 62 Solved: 38
Submit Status Web Board
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
Sample Output
HINT
题目分析:题干里夹杂一堆生僻词(也可能是我英语渣),反正题意晦涩难懂,但是弄明白题意后就好做多了。可以设立一个二维循环数组来模拟每次操作的过程,注意要避开要扔掉的那张牌并记录其位置,把对应的数字放到那个位置里,先把表打出来,然后每次查询即可。
注意不要忘了取模。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int ans[14][20];
int buf[2][20];
void init()
{
ans[1][0]=1;
for(int i=2;i<=13;i++)
{
memset(buf,0,sizeof(buf));
for(int j=0;j<i;j++)
buf[0][j]=j;
int num=i,tims=1,pos=1,temp=0;
while(num>0)
{
for(int j=0;j<num-1;j++)
{
buf[temp^1][j]=buf[temp][(j+tims+1)%num];
}
int s=buf[temp][tims%num];
ans[i][s]=pos++;
temp=temp^1;
num--;
tims++;
}
}
}
int main()
{
init();
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
for(int i=0;i<n-1;i++)
{
cout<<ans[n][i]<<" ";
}
cout<<ans[n][n-1]<<endl;
}
}