EOJ#1679 Card Trick(卡牌诡术)

单点时限: 2.0 sec
内存限制: 256 MB
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, telling the number of test cases to follow. Each case consists of one line containing the integer n.
输出格式
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…
input
2
4
5
output
2 1 4 3
3 1 4 5 2

这是题面。
题目大意是这个样子:魔术师现在有一摞n张牌,全是黑桃(无关紧要),他先将牌堆顶部i张牌依次放回牌堆底部(就是比如2 1 4 3这个样例,先把2放在底部,变成了1 4 3 2,然后翻出来第一张是黑桃A,这时候A已经拿出来了,然后先把4放在最底下,就是:3 2 4,再把3放在最底下就是:2 4 3。大概是这么个操作),然后每一次翻出来的牌一定是黑桃i,给你T组数据,每组数据输出一个能完成这个操作的序列。

这个题有点像约瑟夫环,但是约瑟夫环一定是每隔3个人才砍人,这个不是,两张牌之间的牌数不一定,所以我选择用链表解决

对于链表每一个节点,设立一个前驱指针和后继指针,以及它本身答案,一开始整个链表成环,然后洗牌操作就相当于是用后继指针往后跳,抽牌操作就相当于是给这个节点赋值,然后把节点的前驱b和后继a连在一起(将b的后继指针由指向i修改为指向a,a同理)

最后输出每个结点的值就行了~~,因为数据范围比较小,所以还蛮简单~~

最后上代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define rt int
using namespace std;
inline rt read()
{
 register rt u=0,v=1;register char ch=getchar();
 while(!isdigit(ch)){if(ch=='-')v=-1;ch=getchar();}
 while(isdigit(ch)){u=u*10+ch-'0';ch=getchar();}
 return u*v;
}
rt T,n;
struct node
{
 rt before,card,after;
}ans[20];
int main()
{
 T=read();
 while(T--)
 {
  n=read();
  for(int i=1;i<=n;i++)
  {
   i==1 ? ans[1].before=n : ans[i].before=i-1;
   i==n ? ans[n].after=1 : ans[i].after=i+1;
  }
  rt tmp=1;
  for(int i=1;i<n;i++)
  {
   for(int j=1;j<=i;j++)
    tmp=ans[tmp].after;
   ans[tmp].card=i;
   ans[ans[tmp].before].after=ans[tmp].after;
   ans[ans[tmp].after].before=ans[tmp].before;
   tmp=ans[tmp].after;
  }
  ans[tmp].card=n;
  for(int i=1;i<=n;i++)
  printf("%d ",ans[i].card);
  putchar('\n');
 }
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值