Pagini recente » Cod sursa (job #2379882) | Cod sursa (job #3177433) | Cod sursa (job #2890837) | Cod sursa (job #1748013) | Cod sursa (job #2386443)
#include <bits/stdc++.h>
#define NM 10
using namespace std;
ifstream f("permutari.in");
ofstream g("permutari.out");
int n,top,st[NM];
void Write()
{ for(int i=1; i<=n; i++) g<<st[i]<<' ';
g<<'\n';
}
bool Valid()
{ for(int i=1; i<top; i++)
if(st[i]==st[top]) return 0;
return 1;
}
void BKT()
{ st[++top]=0;
while(top)
{ bool ok=0;
while(!ok && st[top]<n)
{ st[top]++;
ok=Valid();
}
if(!ok) top--;
else
if(top==n) Write();
else st[++top]=0;
}
}
int main()
{ f>>n;
BKT();
return 0;
}