您的位置:首页 > 其它

nyoj 714 Card Trick

2015-04-03 18:19 260 查看

Card Trick

时间限制:1000 ms | 内存限制:65535 KB
难度:3
描述
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 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输出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…样例输入
2


4


5

样例输出
2 1 4 3


3 1 4 5 2


比较简单,n值最大才13.


#include<stdio.h>
int a[15];
int main()
{
    int t,n,i,j,m,k;
    int cishu;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        //最大的两个数顺序先找出 
        if((n-1)&1)
        {
            a
=n-1;
            a[n-1]=n;
        }
        else
        {
            a
=n;
            a[n-1]=n-1;
        }
        //找剩余的 
        for(i=n-2;i>=1;i--)
        {
           a[i]=i;
           cishu=i;
           while(cishu--)
           {
               k=a
;//保存最后一位的值 
               for(j=n;j>=i-1;j--)
               {
                   a[j]=a[j-1];
               }
               a[i]=k;//赋值第一位 
           }
        }
        for(i=1;i<=n;i++)
        {
            if(i>1) printf(" ");
            printf("%d",a[i]);
        }
        printf("\n");
    }
    return 0;
}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: