Cod sursa(job #840463)

Utilizator s4d1ckOrtan Seby s4d1ck Data 22 decembrie 2012 18:37:06
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.55 kb
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int v[8], n;
FILE* out;

void solutie()
{
	for (int i = 0; i<n; i++)
		fprintf(out, "%d ", v[i] + 1);
	fprintf(out, "\n");
}

int valid(int l)
{
	for (int i = 0; i<l; i++)
		if (v[i] == v[l]) return 0;
	return 1;
}

void perm(int l)
{
	if (l == n) solutie();
	else for (int i = 0; i<n; i++)
	{
		v[l] = i;
		if (valid(l)) perm(l+1);
	}
}

int main()
{
	FILE* in = fopen("permutari.in", "r");
	fscanf(in, "%d", &n);
	fclose(in);
	out = fopen("permutari.out", "w");
	perm(0);
	fclose(out);
	return 0;
}