HDU2319 Card Trick

来源:互联网 发布:淘宝公益宝贝是正品吗 编辑:程序博客网 时间:2024/06/03 18:25

Card Trick

                                                       Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


                                                       Total Submission(s): 327    Accepted Submission(s): 162


Problem 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, 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…
 

Sample Input
245
 

Sample Output
2 1 4 33 1 4 5 2
 

Source
Nordic 2006

用数组模拟来做,就是想的时候有点别扭。
#include<stdio.h>#include<string.h>int main(){    int n,i,l;    scanf("%d",&n);    int a[1000];    for(i=1;i<=n;i++)    {        memset(a,0,sizeof(a));        int m;        int j,k;        int cnt=2;        scanf("%d",&m);        if(m==1)            printf("1\n");        else if(m==2)            printf("2 1\n");        else        {            a[2]=1;            j=0;            k=2;            for(;;)            {               ++k;               if(a[k]==0)                   j++;               if(j==cnt+1)               {                 a[k]=cnt;                 cnt++;                 j=0;               }               if(k==m)                 k=0;               if(cnt==m+1)                break;            }            for(l=1;l<m;l++)            printf("%d ",a[l]);            printf("%d\n",a[m]);        }    }    return 0;}
下面是借鉴别人用队列做的
#include<stdio.h>#include<iostream>#include<queue>using namespace std;queue<int>q;int flag;void output(){    int temp=q.front();    q.pop();    while(!q.empty())        output();    if(flag==0)    {        printf("%d",temp);        flag=1;    }    else    printf(" %d",temp);}int main(){    int n;    scanf("%d",&n);    while(n--)    {        int t;        scanf("%d",&t);        while(t>0)        {            q.push(t);            int cnt=t;            t--;            while(cnt--)            {                int temp=q.front();                q.pop();                q.push(temp);            }        }        flag=0;        output();        printf("\n");    }    return 0;}



0 0
原创粉丝点击