Cod sursa(job #891425)

Utilizator harababurelPuscas Sergiu harababurel Data 25 februarie 2013 16:42:27
Problema Generare de permutari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include <iostream>
#include <fstream>
#define cout g
#define nmax 9
using namespace std;

ifstream f("permutari.in");
ofstream g("permutari.out");
int st[nmax], n;

void tipar() {
	for(int i = 1; i <= n; i++) cout<<st[i]<<" ";
	cout<<"\n";
}

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

void back(int k) {
	for(int i=1; i<=n; i++) {
		st[k] = i;
		if(valid(k)) {
			if(k==n) tipar();
			else back(k+1);
		}
	}
}

int main() {
	f>>n;	
	back(1);

	return 0;
}