Cod sursa(job #2829234)

Utilizator tiut_cristianTiut Cristian tiut_cristian Data 8 ianuarie 2022 13:50:43
Problema Generare de permutari Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>

using namespace std;

ifstream fin("permutari.in");
ofstream fout("permutari.out");

void f(int nivel, int k, int sol[])
{
    if (nivel == k+1)
    {
        for (int i = 1; i <= k; i++)
            fout << sol[i];
        fout << endl;
    }
    else
        for (int i = 1; i <= k; i++)
        {
            bool ok = 1;
            int j = 1;
            while(j < nivel && ok)
                if(sol[j] == i)
                    ok = 0;
                else
                    j++;
            if(ok)
            {
                sol[nivel] = i;
                f(nivel+1, k, sol);
            }
        }
}

int main()
{
    int k;
    fin >> k;
    int sol[k+1];
    f(1, k, sol);
    return 0;
}