Advertisement
Manh_LK

Dãy ngoặc

Apr 6th, 2020
1,170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int n, Count, A[21], B[3];
  4. void Try(int i)
  5. {
  6.     for (int j = 1; j < 3; j++)
  7.     {
  8.         A[i] = j; B[j]++; Count++;
  9.         if ((A[i] == 1 && B[1] <= n)|| (A[i] == 2 && B[2] <= B[1]))
  10.         {
  11.             Try(i + 1);
  12.         }
  13.         if (Count == n * 2&&(B[1]==B[2]))
  14.         {
  15.             for (int index = 0; index < 2 * n; index++)
  16.             {
  17.                 if (A[index] == 1) cout << "(";
  18.                 else cout << ")";
  19.             }
  20.             cout << endl;
  21.         }
  22.         Count--; B[j]--;
  23.     }
  24. }
  25. int main()
  26. {
  27.     cin >> n;
  28.     B[2] = 0;
  29.     Count = A[0] = B[1] = 1;
  30.     Try(1);
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement