Advertisement
rotti321

Combinari

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("combinari.in");
  6. ofstream g("combinari.out");
  7.  
  8. int sol[dim],n,nr=0,p;
  9.  
  10. int solutie(int k)
  11. {
  12. return(k==p+1);
  13.  
  14. }
  15. void init(int k)
  16. {
  17. sol[k]=0;
  18. }
  19.  
  20. void tipar()
  21. {
  22. for(int i=1;i<=p;i++){
  23. g<<sol[i]<<" ";
  24. }
  25. g<<endl;
  26. }
  27.  
  28. int valid(int k)
  29. {
  30. for(int i=1;i<=k-1;i++){
  31. if(sol[i]>=sol[k])
  32. return 0;
  33. }
  34. return 1;
  35. }
  36.  
  37. int succesor(int k)
  38. {
  39. if(sol[k]<n){
  40. sol[k]++;
  41. return 1;
  42. }
  43. return 0;
  44. }
  45.  
  46. void citire()
  47. {
  48. f>>n>>p;
  49. }
  50.  
  51. void backtr(int k)
  52. {
  53. if (solutie(k))
  54. tipar();
  55. else{
  56. init(k);
  57. while(succesor(k)){
  58. if(valid(k)){
  59. backtr(k+1);
  60. }
  61. }
  62. }
  63. }
  64. int main()
  65. {
  66. citire(); backtr(1);
  67. return 0;
  68. }
Advertisement
RAW Paste Data Copied
Advertisement