
#include <stdio.h>
#include<stdlib.h>
typedef struct
{
int *stk;
int top;
int size;
} stack;
void initstack(stack *s, int n)
{
s->stk = (int*)malloc((s->size=n) * sizeof(int));
s->top = 0;
}
void copystack(stack *ss, stack *s)
{
int i;
if(ss->stk)
free(ss->stk);
ss->stk =(int*)malloc((ss->size=s->size) * sizeof(int));
ss->top = s->top;
for(i=s->top-1; i>=0; i--)
ss->stk[i] = s->stk[i];
}
void outputstack(stack* s)
{
int i;
for(i=0; i<s->top; i++) printf("%d ", s->stk[i]);
printf("\n");
}
int stackempty(stack* s)
{
return !s->top;
}
void push(stack* s, int x)
{
s->stk[s->top++] = x;
}
int pop(stack* s)
{
return s->stk[--s->top];
}
void stackseq(stack *input, stack *s, stack *output)
{
stack ii, ss, oo;
if(stackempty(input))
{
if(stackempty(s))
outputstack(output);
else
{
push(output, pop(s));
stackseq(input, s, output);
}
}
else
{
if(!stackempty(s))
{
initstack(&ii, 1); copystack(&ii, input);
initstack(&ss, 1); copystack(&ss, s);
initstack(&oo, 1); copystack(&oo, output);
push(&oo, pop(&ss));
stackseq(&ii, &ss, &oo);
}
push(s, pop(input));
stackseq(input, s, output);
}
}
void main()
{
int n,i;
stack input, s, output;
initstack(&input, 20);
initstack(&s, 20);
initstack(&output, 20);
scanf("%d",&n);
for(i=n; i>0; i--)
push(&input, i);
stackseq(&input, &s, &output);
}