第六届河南省赛 zzulioj 1486: Card Trick (模拟)nyoj 714

来源:互联网 发布:淘宝女装店铺推广文案 编辑:程序博客网 时间:2024/05/01 16:28

1486: Card Trick

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 60  Solved: 36

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
//题意:
给你n张牌,让你将这n张牌事先排好序,在如下n次操作后(每次操作得到一张牌)使得这n张牌的顺序为1、2、3.....n。
第一次:将第一张牌放到最后得到一个新的序列,此时第一张牌应该是1,将其拿出。
第二次:将前两张牌拿到最后得到一个新的序列,此时第一张牌应该是2,将其拿出。
第三次:将前三张牌拿到最后得到一个新的序列,此时第一张牌应该是3,将其拿出。
...........
第n次:将前n张牌拿到最后得到一个新的序列,此时第一张牌应该是n,将其拿出。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[15];int vis[15];int main(){int t,n,i,j,k,s;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=0;i<=n;i++)vis[i]=0;i=1;k=1;s=0;while(k<=n){if(vis[i]==0){if(s==k){a[i]=k;k++;s=-1;vis[i]=1;}s++;}i++;if(i>n)i=1;}for(i=1;i<n;i++)printf("%d ",a[i]);printf("%d\n",a[n]);}return 0;}

Source

河南省第六届大学生程序设计竞赛

0 0
原创粉丝点击