当前位置: 首页 > 工具软件 > card one > 使用案例 >

Card Trick

巫新知
2023-12-01

题目描述

The magician shuffles a small pack of cards, holds it face down andperforms the following procedure:

1. The top card is moved to the bottom of the pack. The new top card is dealtface 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 cardis 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 cardsbeforehand (and knows how to give a false shuffle). Your program has todetermine 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 thenumber of test cases to follow. 1 ≤ k ≤ 10  Each case consists of one linecontaining the integer n.  1 ≤ n ≤ 13

输出

For each test case, output a line with the correct permutation of thevalues 1 to n, space separated. The first number showing the top card of thepack, etc…

样例输入

2

4

5

样例输出

2 1 4 3

3 1 4 5 2

 

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;

int main ()
{
	int t,n,i,j,k,l,q,w;
	int a[14];
	scanf("%d",&t);
	while (t --)
	{
			scanf("%d",&n);
			memset(a,0,sizeof(a));
				
		for(k=1,i=-1,j=1;k<=n;k++)
		{
			j++;
			l=0;
			while(l<j){
				i++;
				i%=n;
				l++;
				while(a[i]){
					i++;
					i%=n;
				}
				
			}
			
			a[i]=k;
		}
		for (i = 0; i < n; i ++)
			if (i == 0)
				printf("%d",a[i]);
			else
				printf(" %d",a[i]);
		printf("\n");
	}
		return 0;
}



 类似资料:

相关阅读

相关文章

相关问答