Cod sursa(job #2090961)

Utilizator Teodor.mTeodor Marchitan Teodor.m Data 18 decembrie 2017 21:30:32
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.53 kb
#include <bits/stdc++.h>

using namespace std;

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

int x[9];
int n;

bool valid(int k)
{
	for(int i = 1; i < k; ++i)
		if(x[i] == x[k])
			return false;

	return true;
}

void BK()
{
	int i, k = 1;
	x[1] = 0;
	while(k > 0) {
		while(x[k] < n) {
			x[k]++;
			if(valid(k))
				if(k == n) {
					for(int j = 1; j <= n; ++j)
						fout << x[j] << " ";
					fout << '\n';
				}
				else {
					k++;
					x[k] = 0;
				}
		}
		k--;
	}
}

int main()
{
	fin >> n;

	BK();

	return 0;
}