Advertisement
a53

GENERARE_PERMUTARI

a53
Nov 22nd, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #define NR 10
  4. int x[NR],n;
  5. int folosit[NR];
  6.  
  7. void backr(int k)
  8. {
  9. if(k>n)
  10. {
  11. for(int i=1;i<=n;++i)
  12. cout<<x[i]<<' ';
  13. cout<<'\n';
  14. }
  15. else
  16. for(int i=1;i<=n;++i)
  17. {
  18. x[k]=i;
  19. if(!folosit[x[k]]) /// Daca elementul curent nu e deja folosit
  20. {
  21. folosit[x[k]]=1; /// Marcheaza-l ca folosit
  22. backr(k+1);
  23. /// Genereaza restul permutarii
  24. folosit[x[k]]=0; /// Demarcheaza elementul
  25. }
  26. }
  27. }
  28.  
  29. int main()
  30. {
  31. cout<<"Dati n:";cin>>n;
  32. backr(1);
  33. return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement