The magician shuffles a small pack of cards, holds it face down and performs the following procedure:
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.
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
原文:http://www.cnblogs.com/lovychen/p/3546079.html