Card Trick pku 3032

Card Trick
Time Limit:1000MS  Memory Limit:65536K
Total Submit:936 Accepted:707

Description

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
2
4
5

Sample Output
2 1 4 3
3 1 4 5 2

Source
Nordic 2006 

 

#include <iostream>
#include 
<cstring>
using namespace std;
int n;
int a[16];
int flag[16];
int next(int k)
{
    
if(k<n-1) k++;
    
else k=0;
    
return k;
}

int validnext(int s)
{
    s
=next(s);
    
while(flag[s])
       s
=next(s);
     
return s;      
}

void Solve()
{
  
int i,j;
  memset(flag,
0,sizeof(flag));
  
int set=1;
  i
=-1;
  
  
while(set<=n)
  
{
     j
=set+1;
     
while(j--)
      i
=validnext(i);
     flag[i]
=set;
     
set++;    
  }

  
for(i=0;i<n;i++)
   cout
<<flag[i]<<" ";
   cout
<<endl;
}

int main()
{
 
int cases;
 cin
>>cases;
 
while(cases--)
 
{
  cin
>>n;
  Solve(); 
 }
    
 
return 0;
}

發佈了43 篇原創文章 · 獲贊 3 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章