Cod sursa(job #627432)

Utilizator the_snyper06FMI - ALexandru Mihai the_snyper06 Data 29 octombrie 2011 21:50:41
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include<cstdio>

int n, top, cand;
int st[10];

bool EValid(int top)
{
	int i;
	
	for(i = 1; i < top; i++) 
		if(st[i] == st[top]) return false;
	
	return true;
}

void Afisare(int n)
{
	int i;
	
	for(i = 1; i <= n; i++)
		printf("%d ", st[i]);
	printf("\n");
}

int main()
{
	freopen("permutari.in", "r", stdin);
	freopen("permutari.out", "w", stdout);
	
	scanf("%d", &n);
	top = 1;
	st[top] = 0;
	
	while(top > 0)
	{
		cand = 0;
		while(!cand && st[top] < n)
		{
			st[top]++;
			cand = EValid(top);
		}
		
		if(!cand) top--;
		else if(top == n) Afisare(top);
		else st[++top] = 0;
	}
	
	return 0;
}