Cod sursa(job #857088)

Utilizator Coman95coman cosmin Coman95 Data 17 ianuarie 2013 11:47:57
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <fstream>
using namespace std;

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

int a[16];
int n;

void Back( int x );
bool ok( int x );

int main()
{
    fin >> n;
    Back( 1 );
    fin.close();
    fout.close();
    return 0;
}

void Back( int x )
{
    for( int i = 1; i <= n; ++i )
    {
        a[x] = i;
        if( ok( x ) )
        {
            if( x == n )
            {
                for( int j = 1; j <= n; ++j )
                    fout << a[j] << ' ';
                fout << '\n';
            }
            Back( x + 1 );
        }
    }
}

bool ok( int x )
{
    for( int i = 1; i < x; ++i )
        if( a[x] == a[i] )
            return false;
    return true;
}