这道题的题意大体是说一个魔术师第一次拿一张牌放到下面然后将最上面的那张,拿出是黑桃一,第二次从上面拿两张翻到下面,然后拿出黑桃二,以此类推第N次将上面的N张牌放到下面,拿出黑桃N。然后找到应该怎么排列这些牌才能达到这些效果。
直接模拟是不可以的,因为后面的会影响前面的,但是如果从后向前模拟的话,就不受影响了。
Description
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.
Input
On the first line of the input is a single positive integer, telling the number of test cases to follow. Each case consists of one line containing the integer n.
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#include <stdio.h> #include <string.h> int a[100000]; int main() { int n, m, i, j, p, q; scanf("%d",&n); while(n--) { scanf("%d",&m); p = 0; q = 0; a[p] = m; p++; for(i = m - 1; i > 0; i--) { a[p] = i; p++; for(j = 0; j < i; j++) { a[p] = a[q]; p++; q++; } } for(i = p - 1; i > q; i--) printf("%d ",a[i]); printf("%d\n",a[q]); } return 0; }