Cod sursa(job #903529)

Utilizator TwistedFaithStanescu Jean Alexandru TwistedFaith Data 1 martie 2013 21:50:10
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>

using namespace std;

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

int st[9],N,k=1;

inline bool Succesor()
{
    if(st[k]<N)
    {
        st[k]++;
        return 1;
    }
    return 0;
}

inline bool Valid()
{
    for(int i=1;i<k;i++)
        if(st[i]==st[k])
            return 0;
    return 1;
}

inline void Tipar()
{
    for(int i=1;i<=N;i++)
        fout<<st[i]<<' ';
    fout<<'\n';
}

void Back()
{
    bool AS;
    while(k>0)
    {
        do{}while((AS=Succesor()) && (!Valid()));
        if (AS)
            if(k==N) Tipar();
            else st[++k]=0;
        else k--;
    }
}

int main()
{
    fin>>N;
    Back();
}