Cod sursa(job #1521240)

Utilizator Atomique13Teodorescu Bogdan Atomique13 Data 10 noiembrie 2015 01:39:08
Problema Generare de permutari Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <fstream>
using namespace std;
int x[30],k,as,ev,n;
ifstream f("permutari.in");
ofstream g("permutari.out");
void succesor (int x[30],int k,int &as)
{
    if (x[k]<n)
    {
        as=1;
        x[k]++;
    }
    else
        as=0;
}

void valid (int x[30],int k,int &ev)
{
    int i;
    ev=1;
    for (i=1;i<k;i++)
        if (x[k]==x[i])
            ev=0;
}

void tipar(int x[30],int k)
{
    int i;
    for (i=1;i<=k;i++)
        g<<x[i]<<" ";
    g<<endl;
}
void bk()
{
    k=1;
    x[k]=0;
    while (k>0)
    {
        do
        {
            succesor(x,k,as);
            if (as)
                valid (x,k,ev);
        }
        while (as && !ev);
        if (as)
            if (k==n)
                tipar(x,k);
            else
            {
                k++;
                x[k]=0;
            }
        else
            k--;
    }
}
int main()
{
    f>>n;
    bk();
    return 0;
}