码迷,mamicode.com
首页 > 其他好文 > 详细

河南省第六届大学生程序设计竞赛--Card Trick

时间:2014-11-08 16:51:41      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:algorithm   c++   c语言   acm   水题   

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

2
4
5

Sample Output

2 1 4 3
3 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;
} 


河南省第六届大学生程序设计竞赛--Card Trick

标签:algorithm   c++   c语言   acm   水题   

原文地址:http://blog.csdn.net/u014355480/article/details/40921013

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!