天天看點

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;
}