第六届acm河南省赛——Card Trick 模拟

来源:互联网 发布:garch模型 python 编辑:程序博客网 时间:2024/05/01 10:16

题目链接:Card Trick

Card Trick

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 62  Solved: 38

SubmitStatusWeb 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

2
4
5

Sample Output

2 1 4 3
3 1 4 5 2

HINT

题意:魔术师手上有一堆黑桃牌,每次做魔术的时候会连号的从黑桃A开始取n张牌,计编号就是1到n。然后从1开始每次递增1的取上面的一些牌放到最后,如果不够那些牌就取余。然后翻开最上面的那张牌再扔掉。现在魔术师想保证他翻开的牌恰好就是从1到n的递增序列,问他需要在初始如何摆放这些牌。

题目分析:题干里夹杂一堆生僻词(也可能是我英语渣),反正题意晦涩难懂,但是弄明白题意后就好做多了。可以设立一个二维循环数组来模拟每次操作的过程,注意要避开要扔掉的那张牌并记录其位置,把对应的数字放到那个位置里,先把表打出来,然后每次查询即可。

注意不要忘了取模。

#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;    }}


0 0
原创粉丝点击