Advertisement
rotti321

permutari

Mar 2nd, 2022
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. #define dim 100
  3. using namespace std;
  4.  
  5. ///ifstream f("permutari.in");
  6. ///ofstream g("permutari.out");
  7.  
  8. int sol[dim],n,nr=0,p;
  9.  
  10. void tipar()
  11. {
  12. for(int i=1;i<=n;i++){
  13. cout<<sol[i]<<" ";
  14. }
  15. cout<<endl;
  16. }
  17.  
  18. int valid(int k)
  19. {
  20. for(int i=1;i<=k-1;i++){
  21. if(sol[i]==sol[k])
  22. return 0;
  23. }
  24. return 1;
  25. }
  26.  
  27. int succesor(int k)
  28. {
  29. if(sol[k]<n){
  30. sol[k]++;
  31. return 1;
  32. }
  33. return 0;
  34. }
  35.  
  36. void citire()
  37. {
  38. cin>>n; ///C(n,p);
  39. }
  40.  
  41. void backtr(int k)
  42. {
  43. if (k==n+1)
  44. tipar();
  45. else{
  46. sol[k]=0;
  47. while(succesor(k)){
  48. if(valid(k)){
  49. backtr(k+1);
  50. }
  51. }
  52. }
  53. }
  54. int main()
  55. {
  56. citire(); backtr(1);
  57. return 0;
  58. }
Advertisement
RAW Paste Data Copied
Advertisement