Cod sursa(job #1429568)

Utilizator PlatonVPlaton Vlad PlatonV Data 6 mai 2015 17:54:20
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <stdio.h>

short N, a[10];
FILE* f;
FILE* g;

void back(int n, int pos)
{
    for (int i = 1; i <= N; ++i)
    {
        bool x = false;
        for (int o = 0; o < pos; ++o)
            if (a[o] == i)
                x = true;

        if (!x)
        {
            a[pos] = i;
            if (pos < N - 1)
            {
                back(n, pos + 1);
            }
            else
            {
                for (int k = 0; k < N; k++)
                {
                    fprintf(g, "%d ", a[k]);
                }
                fprintf(g, "\n");
            }
        }
    }
}

int main()
{
    f = fopen("permutari.in", "r");
    g = fopen("permutari.out", "w");

    fscanf(f, "%d", &N);

    back(N, 0);

    return 0;
}