Cod sursa(job #2393215)

Utilizator S_AndyAndrei S S_Andy Data 31 martie 2019 04:18:33
Problema Generare de permutari Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.52 kb
#include <fstream>
#define MAX_N 8

using namespace std;

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


int v[MAX_N + 1], n;
bool u[MAX_N + 1];

void bk(int k) {
	if (k <= n) {
		for (int i = 1; i <= n; ++i) {
			if (u[i]) {
				u[i] = false;
				v[k] = i;
				bk(k + 1);
				u[i] = true;
			}
		}
	}
	else {
		for (int i = 1; i <= n; ++i) {
			fout << v[i] << " ";
		}
		fout << "\n";
	}
}

int main() {
	fin >> n;
	for (int i = 1; i <= n; ++i) {
		u[i] = true;
	}
	bk(0);
}