Cod sursa(job #1696392)

Utilizator Esteban_AlexCihodaru Ciprian-Alexandru Esteban_Alex Data 28 aprilie 2016 23:15:27
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,st[10];

int Valid(int top)
{
    int i;

    for(i=1;i<top;i++)
        if(st[i]==st[top]) return 0;

    return 1;
}

void Afisare(int top)
{
    int i;

    for(i=1;i<=top;i++)
        fout<<st[i]<<" ";
    fout<<"\n";
}

void Back()
{
    int top,cand;

    top=1;
    st[top]=0;

    while(top>0)
    {
        cand=0;
        while(!cand &&st[top]<n)
        {
            st[top]++;
            cand=Valid(top);

        }
        if(!cand) top--;
        else if(top==n) Afisare(top);
        else {
                top++;
                st[top]=0;
             }
    }


}
int main()
{
    fin>>n;
    Back();
    return 0;
}