ny714 Card Trick
Card Trick
时间限制:1000 ms | 内存限制:65535 KB
难度:3
- 描述
-
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 2 #include<deque> 3 #include<iostream> 4 #include<cstring> 5 using namespace std; 6 int main() 7 { 8 deque<int>q; 9 int t,n,a,m,i,k,j; 10 cin>>t; 11 while(t--) 12 {while(!q.empty()) 13 q.pop_front(); 14 cin>>n; 15 int mm=n-1,kk=2; 16 q.push_front(n); 17 for(i=n-1;i>=1;i--,mm--,++kk) 18 { 19 q.push_front(mm); 20 for(j=i%kk;j>0;j--) 21 { 22 q.push_front(q.back()); 23 q.pop_back(); 24 } 25 } 26 for(i=0;i<n;i++) 27 { 28 cout<<q.front()<<" "; 29 q.pop_front(); 30 } 31 cout<<endl; 32 } 33 return 0; 34 } 35