河南省第六屆大學生程式設計競賽--Card Trick,河南省--card

來源:互聯網
上載者:User

河南省第六屆大學生程式設計競賽--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;} 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.