Cod sursa(job #2880520)
Utilizator | Antonio Alexandru Radu RaduAntoneo | Data | 29 martie 2022 20:23:01 |
---|---|---|---|
Problema | Generare de permutari | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.66 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream f("permutari.in");
ofstream g("permutari.out");
int n, sol[9];
bool check(int i) {
for(int index = 1; index < i; index++)
if(sol[i] == sol[index])
return false;
return true;
}
void backtrack(int k) {
for(int i = 1; i <= n; i++) {
sol[k] = i;
if(check(k)) {
if(k == n) {
for(int j = 1; j <= n; j++)
g << sol[j] << " ";
g << '\n';
}
else
backtrack(k + 1);
}
}
}
int main() {
f >> n;
backtrack(1);
return 0;
}