Pagini recente » Cod sursa (job #102722) | Cod sursa (job #240407) | Cod sursa (job #1242388) | Cod sursa (job #1493519) | Cod sursa (job #2241713)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("permutari.in");
ofstream fout("permutari.out");
int st[20] , n;
void Afisare(int top)
{
int i;
for(i = 1; i <= top; i++)
fout << st[i] << " ";
fout << "\n";
}
int Validare(int top)
{
int i;
for(i = 1; i < top; i++)
if(st[i] == st[top])return 0;
return 1;
}
void Backtracking()
{
int cand , top;
top = 1;
st[top] = 0;
while(top > 0)
{
cand = 0;
while(cand == 0 && st[top] < n)
{
st[top]++;
cand = Validare(top);
}
if(cand == 0)top--;
else if(top == n)Afisare(top);
else st[++top] = 0;
}
}
int main()
{
fin >> n;
Backtracking();
return 0;
}