The magician shuffles a small pack of cards, holds it face down and performs the following procedure:
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.
2
4
5
2 1 4 3
3 1 4 5 2
模拟!
AC码:
#include<stdio.h> int main() { int T,n,i,k,step; int visit[15],num[15]; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=0;i<=n;i++) visit[i]=0; k=1; i=1; step=0; while(k<=n) { if(visit[i]==0) { if(step==k) { num[i]=k; k++; step=-1; visit[i]=1; } step++; } i++; if(i>n) i=1; } for(i=1;i<=n;i++) printf("%d ",num[i]); printf("\n"); } return 0; }
原文地址:http://blog.csdn.net/u012804490/article/details/24785637