Advertisement
rofllmaolol

Combinari

Feb 26th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int k,n,p,st[10];
  4.  
  5. void init()
  6. {
  7.     st[k]=0;
  8. }
  9.  
  10. int succesor()
  11. {
  12.     if(st[k]<n)
  13.     {
  14.         st[k]++;
  15.         return 1;
  16.     }
  17.     return 0;
  18. }
  19.  
  20. int valid()
  21. {
  22.     int i;
  23.     for(i=1;i<k;i++)
  24.         if(st[i]==st[k])
  25.         return 0;
  26.     return 1;
  27. }
  28.  
  29. int solutie()
  30. {
  31.    int ok=0,i,nr=0;
  32.     if(k==p)
  33.     {
  34.         for(i=2;i<=k;i++)
  35.         {
  36.             if(st[i-1]<st[i])
  37.                 nr++;
  38.         }
  39.         if(nr==k-1)
  40.             return 1;
  41.         else
  42.             return 0;
  43.     }
  44.     else
  45.         return 0;
  46. }
  47.  
  48. void tipar()
  49. {
  50.     int i;
  51.     for(i=1;i<=p;i++)
  52.         cout<<st[i];
  53.     cout<<endl;
  54. }
  55.  
  56. void backt()
  57. {
  58.     int as;
  59.     k=1;
  60.     init();
  61.     while(k>0)
  62.     {
  63.         do
  64.         {
  65.  
  66.         }while((as=succesor()) and !valid());
  67.         if(as)
  68.             {if(solutie())
  69.                 {tipar();}
  70.             else
  71.             {
  72.                 k++;
  73.                 init();
  74.             }}
  75.         else
  76.             k--;
  77.     }
  78. }
  79.  
  80. main()
  81. {
  82.     cin>>n>>p;
  83.     backt();
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement