0%

北大 acm 3032 Card Trick 解题报告

Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2203 Accepted: 1630

Description

Alt text
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.

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>

using namespace std;

void jian(int k,int n,int a[]) //往前移
{
int i;

for(i=k;i<n;i++)
a[i]=a[i+1];

a[n]=0;
}

int main()
{
int t,n,i,k,s,to,a[15]={0},postion[15]={0};

cin>>t;

while(t--)
{
cin>>n;
if(n==1)
{
cout<<1<<endl;
continue;
}

for(i=1;i<=n;i++)
a[i]=i; //给每个顺序初值

k=2; //k代表位置从2开始
to=2; //下个位置的间隔
s=n; //总的牌数

for(i=1;i<=n;i++)
{
postion[i]=a[k]; //顺序为i的牌位置为a[k]

jian(k,s,a); //位置a往前移一位

s--; //抽走牌后总牌数减1
k+=to++; //下一个牌的位置
if(s==0)
break;

while(k>s)
k=k-s;
}

for(i=1;i<=n;i++)
for(k=1;k<=n;k++)
if(postion[k]==i)
{
cout<<k<<" ";
postion[k]=0;
break;
}

cout<<endl;
}
return 0;
}
谢谢您的打赏,我的大英雄 ^_^
Thank you for your generosity, my big hero ^_^

更多内容请关注公众号「西小玛」