Cod sursa(job #3244093)

Utilizator Mateixx1Trandafir matei Mateixx1 Data 23 septembrie 2024 16:09:09
Problema Generare de permutari Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("permutari.in");
ofstream g("permutari.out");
int v[9],n;

bool valid(int k) {
    for(int i=1; i<=k-1; i++) {
        if(v[i]==v[k]) {
            return 0;
        }
    }
    return 1;
}

void tipar(int k) {
    for(int i=1; i<=k; i++) {
        g<<v[i]<<' ';
    }
    g<<"\n";
}

void bk() {
    int k;
    bool gasit=0;
    k=1;
    v[k]=0;
    while(k>0) {
        gasit=0;
        while(v[k]<n&&!gasit) {
            v[k]++;
            gasit=valid(k);
        }
        if(gasit) {
            if(k==n) {
                tipar(k);
            } else {
                k++;
                v[k]=0;
            }
        } else {
            k--;
        }
    }
}

int main() {
    f>>n;
    bk();
    f.close();
    g.close();
    return 0;
}