Mai intai trebuie sa te autentifici.
Cod sursa(job #1254316)
| Utilizator | Data | 2 noiembrie 2014 15:30:09 | |
|---|---|---|---|
| Problema | Generare de permutari | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.79 kb |
#include <iostream>
#include <fstream>
using namespace std;
int n,k;
int st[20];
ifstream fin("permutari.in");
ofstream fout("permutari.out");
void Afisare(int top)
{
int i;
for(i=1;i<=top;i++)
fout<<st[i]<<" ";
fout<<"\n";
}
int Valid(int top)
{ int i;
for(i=1;i<=top-1;i++)
if(st[i]==st[top]) return 0;
return 1;
}
void Back()
{
int cand,top ;
top=1; st[top]=0;
while (top>0)
{
cand=0;
while(!cand && st[top]<n)
{
st[top]++;
cand=Valid(top);
}
if(!cand ) top--;
else
if(top==n) Afisare(top);
else
{
top++;
st[top]=0;
}
}
}
int main()
{
fin>>n;
Back();
return 0;
}
