Problem:
Permutation Enumeration

For given an integer $n$, print all permutations of $\{1, 2, ..., n\}$ in lexicographic order.

Input

An integer $n$ is given in a line.

Output

Print each permutation in a line in order. Separate adjacency elements by a space character.

Constraints

$1 \leq n \leq 9$

Sample Input 1

2

Sample Output 1

1 2
2 1

Sample Input 2

3

Sample Output 2

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Solution in C (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int *numbers, *used, n;

void sequence(int m)
{
  int i;
  if (n==m) {
    for (i=0; i<n-1; i++)
      printf("%d ", numbers[i]);
    printf("%d\n", numbers[i]);
  } else {
    for (int i=0; i<n; i++) {
      if (used[i]) continue;
      numbers[m] = i+1;
      used[i]=1;
      sequence(m+1);
      used[i]=0;
    }
  }
}


int main()
{
  fscanf(stdin, "%d", &n);
  numbers = (int*)malloc(sizeof(int)*n);
  used = (int*)malloc(sizeof(int)*n);
  for (int i=0; i<n; i++)  used[i]=0;
  sequence(0);
}