Cod sursa(job #1387475)

Utilizator RonaldoPop Sebastian Paul Ronaldo Data 14 martie 2015 11:35:19
Problema Generare de permutari Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("permutari.in");
ofstream g("permutari.out");
int st[100], n;
void init (int p){
    for (int i=1; i<=25; i++)
        st[i]=0;
}
bool valid(int p){
    bool ok=true;
    for (int i=1; i<=p-1; i++)
        if (st[i]==st[p]) ok=false;
    return ok;
}

void tipar(int p){
    for (int i=1; i<=p; i++)
            g<<st[i]<<" ";
        g<<endl;
}
void backk(int p){
    p=1;
    st[p]=0;
    while (p>0){
       if (st[p]<n) { st[p]=st[p]+1;
        if (valid(p)==true)
            if (p==n) tipar(p);
                    else {
                p++;
                st[p]=0;
            }
       }
        else p--;

    }
}
int main()
{   f>>n;
    init(n);
    backk(1);
    return 0;
}