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>
void enumeration(int result[],int used[],int n,int d){
    int i;
    if(d>=n){
        for(i=0;i<n;i++){
            if(i==0)printf("%d",result[i]);
            else printf(" %d",result[i]);
        }
        printf("\n");
        return;
    }
    
    int used2[n-d-1];
    int j,k;
    for(i=0;i<n-d;i++){
        result[d]=used[i];
        for(j=0,k=0;j<n-d;j++)if(j!=i)used2[k++]=used[j];
        enumeration(result,used2,n,d+1);
    }
    
    return;
}
int main(void){
    int i;
    
    int n;
    scanf("%d\n",&n);
    
    int used[n];
    for(i=0;i<n;i++)used[i]=i+1;
    
    int result[n];
    
    enumeration(result,used,n,0);
    
    return 0;
}