描述
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.
-
输入
- On the first line of the input is a single positive integer k, telling the number of test cases to follow. 1 ≤ k ≤ 10 Each case consists of one line containing the integer n. 1 ≤ n ≤ 13 输出
- 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… 样例输入
-
2 4 5
样例输出
-
2 1 4 3 3 1 4 5 2
来源
- 第六届河南省程序设计大赛
-
1 #include <iostream> 2 using namespace std; 3 void roll_left(int k,int n,int a[]) { //往前移 4 int i; 5 for(i=k; i<n; i++) 6 a[i]=a[i+1]; 7 a[n]=0; 8 } 9 int main() { 10 int t,n,i,k,s,to,a[15]= {0},pos[15]= {0}; 11 cin>>t; 12 while(t--) { 13 cin>>n; 14 if(n==1) { 15 cout<<1<<endl; 16 continue; 17 } 18 for(i=1; i<=n; i++) a[i]=i; //给每个顺序初值 19 k=2; //k代表位置从2开始 20 to=2; //下个位置的间隔 21 s=n; //总的牌数 22 for(i=1; i<=n; i++) { 23 pos[i]=a[k]; //顺序为i的牌位置为a[k] 24 roll_left(k,s,a); //位置a往前移一位 25 s--; //抽走牌后总牌数减1 26 k+=to++; //下一个牌的位置 27 if(s==0) break; 28 while(k>s) k=k-s; 29 } 30 int cnt=0; 31 for(i=1; i<=n; i++) 32 for(k=1; k<=n; k++) 33 if(pos[k]==i) { 34 if(cnt==n-1) cout<<k<<endl; 35 else cout<<k<<" "; 36 pos[k]=0; 37 cnt++; 38 break; 39 } 40 } 41 return 0; 42 }