Cod sursa(job #194391)

Utilizator cristi8Constantin-Cristian Balas cristi8 Data 10 iunie 2008 12:34:34
Problema Generare de permutari Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.79 kb
/*
  Cristi Balas,
  Facultatea de Matematica si Informatica, Universitatea Bucuresti
  cristi8 [at] gmail.com
*/
#include <stdio.h>

int n;
int used[10];
int stiva[10];

void afiseaza() {
    int i;
    for(i = 1; i <= n; i++)
        printf("%d ", stiva[i]);
    printf("\n");
}

void back(int nivel)
{
    if(nivel > n)
        afiseaza();
    else
    {
        for(stiva[nivel] = 1; stiva[nivel] <= n; stiva[nivel]++)
            if(!used[ stiva[nivel] ])
            {
                used[ stiva[nivel] ] = 1;
                back(nivel + 1);
                used[ stiva[nivel] ] = 0;
            }
    }
}

int main()
{
    freopen("permutari.in", "r", stdin);
    freopen("permutari.out", "w", stdout);
    scanf("%d", &n);
    back(1);
    return 0;
}