Cod sursa(job #2574523)

Utilizator nTropicManescu Bogdan Constantin nTropic Data 5 martie 2020 23:19:00
Problema Generare de permutari Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>

using namespace std;

const int len = 100;
int n, v[len];
bool ok;

void bkt(int p) {
    if (p == n) {
        for (int i = 1; i <= n; i++)
            cout << v[i] << " ";
        cout << "\n";
    }
    else
        for (int i = 1; i <= n; i++) {
            ok = true;
            for (int j = 1; j <= p; j++)
                if (v[j] == i)
                    ok = false;
            if (ok) {
                v[p + 1] = i;
                bkt(p + 1);
            }
        }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    freopen("permutari.in", "r", stdin);
    freopen("permutari.out", "w", stdout);

    cin >> n;
    bkt(0);
}