Advertisement
a53

Combinari

a53
Oct 8th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <fstream>
  2. #define NR 16
  3. using namespace std;
  4. ifstream fin("combinari.in");
  5. ofstream fout("combinari.out");
  6. int x[NR],n,p;
  7.  
  8. void init(int k)
  9. {
  10. x[k]=0;
  11. }
  12. bool succesor(int k)
  13. {
  14. if(x[k]<n-p+k)
  15. return true;
  16. else
  17. return false;
  18. }
  19.  
  20. bool continuare(int k)
  21. {
  22. if(k>1)
  23. if(x[k]<x[k-1])
  24. return false;
  25. for(int i=1;i<=k-1;++i)
  26. if (x[k]==x[i])
  27. return false;
  28. return true;
  29. }
  30.  
  31. bool solutie(int k)
  32. {
  33. if(k==p+1)
  34. return true;
  35. else
  36. return false;
  37. }
  38.  
  39. void afisare()
  40. {
  41. for(int i=1;i<=p;++i)
  42. fout<<x[i]<<' ';
  43. fout<<'\n';
  44. }
  45.  
  46. void backtracking()
  47. {
  48. int k=1;
  49. init(1);
  50. while(k!=0)
  51. if(solutie(k))
  52. afisare(),--k;
  53. else
  54. if(succesor(k))
  55. {
  56. ++x[k];
  57. if(continuare(k))
  58. ++k;
  59. }
  60. else
  61. init(k),--k;
  62. }
  63.  
  64. int main()
  65. {
  66. fin>>n>>p;
  67. backtracking();
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement