Cod sursa(job #841795)

Utilizator Brz_VladBrezae Vlad Brz_Vlad Data 24 decembrie 2012 22:56:45
Problema Generare de permutari Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.69 kb
#include <stdio.h>

int n;

int st[11];

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

int main(int argc, char* argv[])
{
	freopen("permutari.in","r",stdin);
	freopen("permutari.out","w",stdout);
	
	scanf("%d",&n);
	
	int k=1;
	
	int j;
	
	unsigned status = 0;
	
	while(k>0){
		j = st[k]+1;
		while( j<=n && (status & (1<<j)) ){
			j++;
		}
		
		if( j == n+1 ){
			status = status & (~(1<<st[k]));
			st[k] = 0;
			k--;
		}		
		else{
			status = status & (~(1<<st[k]));
			st[k] = j;
			status = status | (1<<st[k]);
			if( k == n ){
				printSol();
				
				status = status & (~(1<<st[k]));
				st[k] = 0;
				k--;
			}
			else{
				k++;
			}			
		}
	}
	
	return 0;
}