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
-
题意:在一堆n张扑克牌中,第1次先将顶部1张扑克牌放在底部,然后取出顶部的扑克牌应该为黑桃1,从顶部的取两次扑克牌放在底部,在取出顶部的扑克牌应该为黑桃2,同理3次......n次之后取出的扑克牌顺序应该是1......n。
-
解法:用队列模拟这个取牌的过程,反过来推。即由原始顺序1到n推出答案。
-
#include<stdio.h> #include<queue> #include<stack> using namespace std; int main() { int t,n,i; queue<int>q,res; stack<int>s; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=n;i>=1;i--) { q.push(i); int c=i; while(c--) { int a=q.front(); q.pop(); q.push(a); } } while(!q.empty()) { s.push(q.front()); q.pop(); } while(!s.empty()) { printf("%d ",s.top()); s.pop(); } printf("\n"); } return 0; }