天天看点

SPOJ 1108. Card Trick 模拟题1108. Card Trick

题目描述:

SPOJ Problem Set (classical)

1108. Card Trick

Problem code: CTRICK

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 ≤ 20000.

Input

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. 

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…

Example

Input:
2
4
5

Output:
2 1 4 3
3 1 4 5 2      

        第一种思路算是  找规律吧。首先1一定是放在2这个位置的,第i张牌一定是放在第i-1张牌右数第i+1个空位置。但是要注意优化,

int b=i%(n-i+1)+1;
                if(temp==b)
                {
                    a[j]=i;
                    flag=j;
                    break;
                }
           

              对于上面优化代码比较好理解是:

                 int b;

                if((i+1)%(n-i+1)==0)

                    b=n-i+1;

                else b=(i+1)%(n-i+1);  n-i+1表示当前剩下的空格,要把i放到第i+1个空格。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    //freopen("in.txt","r",stdin);
    int cas,n,a[20011];
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        a[2]=1;
        int flag=2;
        for(int i=2; i<n; i++)
        {
            int temp=0;
            for(int j=flag+1; ; j++)
            {
                if(j==n+1)
                    j=1;
                if(a[j]==0)
                    temp++;
                int b=i%(n-i+1)+1;
                if(temp==b)
                {
                    a[j]=i;
                    flag=j;
                    break;
                }
            }
        }
        if(a[1]==0)
            a[1]=n;
        cout<<a[1];
        for(int i=2; i<=n; i++)
        {
            if(a[i]==0)
                a[i]=n;
            cout<<" "<<a[i];
        }
        cout<<endl;
    }
    return 0;
}
           

         下面是用双端队列做的,就是模拟题目描述的逆过程。同样是优化一下比较好。

#include<cstdio>
#include<cstring>
#include<vector>
#include<deque>
#include<iostream>
using namespace std;
int main()
{
    int cas,n,seat;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d",&n);
        deque<int>c;
        c.clear();
        for(int i=n; i>0; i--)
        {
            int cur=i;
            c.push_front(i);
            seat=c.size();
            cur=cur%seat;
            while(cur--)
            {
                int a=c.back();
                c.pop_back();
                c.push_front(a);
            }
        }
        for(int i=0; i<n; i++)
        {
            int a= c.front();
            cout<<a;
            c.pop_front();
            if(i!=n-1)
            cout<<" ";
        }
        cout<<endl;
    }
    return 0;
}