Cod sursa(job #2592134)

Utilizator KillHorizon23Orban Robert KillHorizon23 Data 1 aprilie 2020 11:11:38
Problema Generare de permutari Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>
using namespace std;

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

int n;
vector < int > v(10);

bool valid(int poz)
{
    for (int i = 1; i < poz; ++i)
        if (v[i] == v[poz])
            return false;
    return true;
}

void afis()
{
    for (int i = 1; i <= n; ++i)
        fout << v[i] << " ";
    fout << "\n";
}

void Back(int p)
{
    if (p == n + 1)
        afis();
    else
        for(int i = 1; i <= n; ++i)
        {
            v[p] = i;
            if (valid(p))
                Back(p + 1);
        }
}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(0);
    fin >> n;
    Back(1);
}