POJ-3032 Card Trick

来源:互联网 发布:学粤语歌软件 编辑:程序博客网 时间:2024/05/16 13:45

原题链接

Card Trick
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 4096
Accepted: 2931

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, 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

245

Sample Output

2 1 4 33 1 4 5 2


最后的排列肯定是1 2 3...n,所以从结果开始反向模拟。

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <stack>#include <queue> #include <cmath>#include <algorithm>#define maxn 10005 #define INF 1e9typedef long long ll;using namespace std;int num[20];int main(){//freopen("in.txt", "r", stdin); int t;scanf("%d", &t);while(t--){int n;scanf("%d", &n);for(int i = 1; i <= n; i++)    num[i] = i;    for(int i = n-1; i >= 1; i--){    int h = i % (n - i + 1);    while(h--){    int d = num[n];    for(int j = n-1; j >= i; j--)      num[j+1] = num[j];    num[i] = d;    }    }    printf("%d", num[1]);    for(int i = 2; i <= n; i++)      printf(" %d", num[i]);    puts("");}return 0;}


0 0
原创粉丝点击