fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int n, d = 1;
  5. char a[10];
  6.  
  7. void pr() {
  8. cout<< d<< ": ";
  9. for (int i = 0; i < n; i++)
  10. cout << a[i] << " ";
  11. cout << endl;
  12. d++;
  13. }
  14.  
  15. void permutation(int i) {
  16. for (char c : {'A', 'B'}) {
  17. if (i == 0 || a[i - 1] != 'B' || c != 'B') { // Kiểm tra không có 2 chữ B cạnh nhau
  18. a[i] = c;
  19. if (i == n - 1)
  20. pr();
  21. else
  22. permutation(i + 1);
  23. }
  24. }
  25. }
  26.  
  27. int main() {
  28. cin >> n;
  29. permutation(0);
  30. }
  31.  
Success #stdin #stdout 0.01s 5280KB
stdin
4
stdout
1: A A A A 
2: A A A B 
3: A A B A 
4: A B A A 
5: A B A B 
6: B A A A 
7: B A A B 
8: B A B A