Cod sursa(job #1902768)

Utilizator alexandra_paticaAndreea Alexandra Patica alexandra_patica Data 4 martie 2017 19:37:24
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include <cstdio>
using namespace std;

int n, st[10];
int valid (int k)
{
    for (int i=1; i<k; i++)
        if (st[i]==st[k]) return 0;
    return 1;
}

void permut (int k)
{
    for (int x=1; x<=n; x++){
        st[k]=x;
        if (valid(k)){
            if (k==n){
                for (int i=1; i<=k; i++)
                    printf("%d ", st[i]);
                printf("\n");
            }
            else permut(k+1);
        }
    }
}

int main (){
    freopen("permutari.in", "r", stdin);
    freopen("permutari.out", "w", stdout);

    scanf("%d", &n);
    permut(1);
    return 0;
}