Advertisement
STANAANDREY

combi cls

Oct 20th, 2020
2,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int st[27], n, p;
  4.  
  5. void tipar(int k)
  6. {
  7.     for (int i = 1; i <= k; i++)
  8.         cout << (char)(st[i] + 64);
  9.     cout << endl;
  10. }
  11.  
  12. int solutie(int k)
  13. {
  14.     return k == p;
  15. }
  16.  
  17. int validare(int k)
  18. {
  19.     if (k > 1 && st[k] < st[k - 1])
  20.         return 0;
  21.     for (int i = 1; i < k; i++)
  22.         if (st[i] == st[k])
  23.             return 0;
  24.     return 1;
  25. }
  26.  
  27. void bktr()
  28. {
  29.     int k = 1;
  30.     st[k] = 0;
  31.     while (k)
  32.     {
  33.         if (st[k] < n)
  34.         {
  35.             st[k]++;
  36.             if (validare(k))
  37.             {
  38.                 if (solutie(k))
  39.                     tipar(k);
  40.                 else
  41.                 {
  42.                     k++;
  43.                     st[k] = 0;
  44.                 }
  45.             }
  46.         }
  47.         else
  48.             k--;
  49.     }
  50. }
  51.  
  52. int main ()
  53. {
  54.     cin >> n >> p;
  55.     bktr();
  56.     return 0;
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement