河南省第六屆大學生程式設計競賽--Card Trick,河南省--card
Card Trick
Time Limit: 2 Seconds Memory Limit: 64 MB
Description
The magician shuffles a small pack of cards, holds it face down and performs the following procedure:
1. 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.
2. 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.
3. Three cards are moved one at a time…
4. 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 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
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
245
Sample Output
2 1 4 33 1 4 5 2
Source
河南省第六屆大學生程式設計競賽
以前省賽的題目還是蠻水的!!
題意:一疊牌 如:2 1 4 3 ,把最上面1張牌(也就是2)放到底下去,這時牌序為(1 4 3 2)翻開第一張是1(A),把A拿走,牌序(4 3 2),把最上面2張牌(也就是4 3)放到底下去(2 4 3),翻開第一張是2,把2拿走(4 3)然後4 3進行3次交換後,翻開第一張牌是3。
第二個是:把最上面的1張牌(3)放到最下面去(1 4 5 2 3),翻開第一張是1,把1拿掉(4 5 2 3)把上面2張放到底下去(2 3 4 5)翻開第一張是2,把2拿掉(3 4 5)把上面3張放到底下去還是(3 4 5)翻開第一張是3,把3 拿掉(4 5)把上面4張放到底下去(可以迴圈抽放)(4 5)翻開第一張是4.。
思路: 就寫個隊列,然後把隊列頭放到隊列尾!!迴圈幾次就好了!!不懂可以看代碼!!
AC代碼:
#include <cstdio>#include <iostream> #include <cstring>#include <cstdlib>#include <cmath>#include <stack>#include <queue>#include <algorithm>#include <string>using namespace std;int main(){queue<int> q;int k, n, a[20];scanf("%d", &k);while(k--){scanf("%d", &n);q.push(n);for(int i=n-1; i>=1; i--){q.push(i);for(int j=0; j<i; j++){q.push(q.front());q.pop();}}for(int i=0; i<n; i++){a[i] = q.front(); q.pop(); }for(int i=n-1; i>0; i--){printf("%d ", a[i]);}printf("%d\n", a[0]);}return 0;}