HDU:2319 Card Trick

来源:互联网 发布:天津淘宝图片拍摄 编辑:程序博客网 时间:2024/06/05 07:26

Card Trick

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 273    Accepted Submission(s): 132


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题目意思:一副牌,有1~13,这些牌,按题目所给规则翻拍后,牌被取走的顺序为1~n例如:2 1 4 3,先将前1张牌放到最后面,序列为1 4 3 2,然后取走1,剩余 4 3 2,然后在挪动两次牌,顺序变为2 3 4,然后取走2,剩余3 4,然后挪动3次牌,变为3 4,拿走3,剩余4,牌被拿走的顺序为1 2 3 4,让求该顺序所对应的牌的原始序列。题目代码:#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>using namespace std;int main(){    int m,n;//m组测试数据    int a[15],b[15];    cin>>m;    while(m--)    {        cin>>n;//测试数据        queue<int>qu;//定义一个队列        for(int i = 1; i <= n; i++)//将1~n入队,i代表牌的位置        {            qu.push(i);        }        for(int i = 1; i <= n; i++)//求n张牌的位置        {            int temp = i;//翻牌次数            while(temp--)            {                int x = qu.front();                qu.pop();                qu.push(x);            }            a[i] = qu.front();//保留队首元素的值,即为i在牌中的初始次序            qu.pop();        }        for(int i = 1; i <= n; i++)        {            b[a[i]] = i;//a[i]为i在牌中位置        }        cout<<b[1];        for(int i = 2; i <= n; i++)        {            cout<<" "<<b[i];        }        cout<<endl;    }    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 两岁宝宝打喷嚏怎么办 三岁宝宝打喷嚏怎么办 鼻子痒流鼻涕打喷嚏怎么办 鼻子痒流清鼻涕打喷嚏怎么办 老觉得鼻孔干燥怎么办 鼻子突然很痒怎么办 鼻孔里面很痒怎么办 孩子鼻梁肿了怎么办 鼻子外面痒痒的怎么办 孩子鼻梁子磕到硬物上肿了怎么办 过敏鼻炎犯了怎么办 鼻子外面很痒怎么办 鼻子里面很痒怎么办 鼻子外面烂了怎么办 上火鼻子烂了怎么办 孕妇鼻痒打喷嚏怎么办 鼻子内痒流鼻涕怎么办 鼻腔内红肿发炎怎么办 鼻子痒喉咙干怎么办 坐月子鼻子痒打喷嚏怎么办 鼻子下面烂了怎么办 坐月子鼻子不通气怎么办 感冒鼻子痒打喷嚏怎么办 鼻子干燥痒是怎么办 鼻子干又痒怎么办 鼻子太干燥了怎么办 鼻子发痒有血丝怎么办 喷嚏打个不停怎么办 感冒初期鼻子痒怎么办 鼻炎感冒鼻痒怎么办 经期感冒鼻子痒怎么办 感冒鼻子外面痒怎么办 孕妇感冒鼻子痒怎么办 怀孕2个月犯鼻炎怎么办 鼻炎痒的难受怎么办 鼻子干痒出血怎么办 喷嚏发不出来怎么办 鼻子过敏打喷嚏流鼻涕怎么办 孩子老打喷嚏流鼻涕怎么办 流鼻涕流清水样怎么办 鼻炎流鼻涕怎么办速效办法