Pagini recente » Cod sursa (job #2153125) | Cod sursa (job #2150497) | Cod sursa (job #1917593) | Cod sursa (job #2321530) | Cod sursa (job #2152636)
#include <iostream>
#include <fstream>
using namespace std;
unsigned short int n, x[9];
ifstream fin("permutari.in");
ofstream fout("permutari.out");
bool Candidate(unsigned short int y) {
for (unsigned short int i = 1; i < y; i++) if (x[i] == x[y]) return false;
return true;
}
void BackTrack(unsigned short int y) {
unsigned short int i, j;
for (i = 1; i <= n; i++) {
x[y] = i;
if (Candidate(y)) {
if (y == n) {
for (j = 1; j <= n; j++) {
fout << x[j] << " ";
}
fout << "\n";
} else BackTrack(y + 1);
}
}
}
int main()
{
fin >> n;
BackTrack(1);
return 0;
}