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

题目链接:Card Trick

Card Trick

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值