Cod sursa(job #1839713)

Utilizator lorena1999Marginean Lorena lorena1999 Data 3 ianuarie 2017 13:03:58
Problema Generare de permutari Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f("permutari.in");
ofstream g("permutari.out");

int n, stiv[10000];

int verif_0()
    {
        for(int i=1; i<=n; i++)
            if(stiv[i]==0)
                return 0;
        return 1;
    }

int verif_dubl()
    {
        for(int i=1; i<n; i++)
            for(int j=i+1; j<=n; j++)
                if(stiv[i]==stiv[j])
                    return 0;
        return 1;
    }
int vect[1000];

void copiere()
    {
        for(int i=1; i<=n; i++)
            vect[i]=stiv[i];
    }

int verif_inainte()
    {
        for(int i=1; i<=n; i++)
            if(stiv[i]!=vect[i])
                {
                    copiere();
                    return 1;
                }
        return 0;
    }

void tipar()
    {
        for(int i=1; i<=n; i++)
            g<<stiv[i]<<" ";
        g<<endl;
    }

void bk(int p)
    {
        if(p>n)
            return;
        for(int i=1; i<=n; i++)
        {
            stiv[p]=i;
            if(verif_0() && verif_dubl() && verif_inainte())
            {
                tipar();
                bk(p+1);
            }
            else bk(p+1);
        }
    }

int main()
    {
        f>>n;
        bk(1);
    }