Cod sursa(job #3304560)

Utilizator robigiirimias robert robigi Data 24 iulie 2025 23:08:11
Problema Generare de permutari Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>

using namespace std;

void permutari(int n, int pos, int* perm, ofstream& fout)
{
    if (pos > n)
    {
        for (int i = 1; i <= n; ++i)
        {
            fout << perm[i] << " ";
        }
        fout << endl;
    }
    for (int i = pos; i <= n; ++i)
    {
        swap(perm[pos], perm[i]);  // we only care about what is in perm[pos] so we swap the first element in first then the 2nd then the 3rd element
        permutari(n, pos + 1, perm, fout);
        swap(perm[pos], perm[i]);
    }
}

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

    int n = 0;
    fin >> n;
    int* used = new int[9];
    int* perm = new int[9];
    for (int i = 0; i <= n; ++i)
        perm[i] = i;

    permutari(n, 1, perm, fout);
}