Advertisement
STANAANDREY

sda pb 2 past exam 1

Jan 13th, 2024
1,478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #define N 10
  5.  
  6. inline int toInt(char ch) {
  7.     return ch - 'a';
  8. }
  9.  
  10.  
  11. int fr[3] = { 3, 4, 3 };
  12. char s[N];
  13. int cnt = 0;
  14. void bktr(int k) {
  15.     if (k == N) {
  16.         for (int i = 0; i < N; i++) {
  17.             printf("%c", s[i]);
  18.         }
  19.         puts("");
  20.         cnt++;
  21.         if (cnt == 5) {
  22.             exit(EXIT_SUCCESS);
  23.         }
  24.         return;
  25.     }
  26.  
  27.     for (char ch = 'a'; ch <= 'c'; ch++) {
  28.         if (fr[toInt(ch)] != 0) {
  29.             fr[toInt(ch)]--;
  30.             s[k] = ch;
  31.             bktr(k + 1);
  32.             fr[toInt(ch)]++;
  33.         }
  34.     }
  35. }
  36.  
  37. int main(void) {
  38.     bktr(0);
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement