Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4096 | Accepted: 2931 |
Description
![](https://i-blog.csdnimg.cn/blog_migrate/61ce3c309ebf30f4b9e6e3aafc8de6f1.png)
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
最后的排列肯定是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 1e9
typedef 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;
}