代码一、
#include<stdio.h>
#define maxn 10010
int data[maxn];
void build(int i, int j)
{
if(i == j)
return ;
int cdata[maxn];
int s = i, p;
for(int k = 0; k <= 1; k++)
{
p = s - 1;
for(int w = k + i; w <= j; w +=2)
cdata[s++] = data[w];
}
for(int k = i; k <= j; k++)
data[k] = cdata[k];
build(i, p);
build(p+1, j);
}
int main()
{
int n, i;
while(scanf("%d", &n), n)
{
for(i = 1; i <= n; i++)
data[i] = i - 1;
build(1,n);
printf("%d:",n);
for(i = 1; i <= n; i++)
printf(" %d", data[i]);
printf("\n");
}
return 0;
}
代码二、
#include <iostream>
#define MAXSIZE 20000
using namespace std;
int a[MAXSIZE],b[MAXSIZE];
int n;
void dixt(int,int);
int main()
{
int i,j;
while((cin >> n) != 0)
{
for(i = 1; i < n; i++)
{
a[i] = i;
}
dixt(0,n-1);
cout << n << ": ";
for(i = 1; i < n; i++)
{
cout << a[i];
}
cout << endl;
}
return 0;
}
void dixt(int x,int y)
{
int i,j;
if(y -x == 1)return ;
for(int i = x; i <= y; i++)
{
b[i] = a[i];
}
for(i = x,j = x; j <= y; j+=2,i++ )
{
a[i] = b[j];
}
for(j = x + 1; j <= y; j+=2)
{
a[i] = b[j];
}
dixt((x+y)/2 + 1,y);
dixt(x,(x+y)/2);
}