Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int n, st[30];
  5. /**lg. stiva = n; st[i] = 1, 2; nr1 >= nr2; nr1 <= n/2;*/
  6. void Afisare()
  7. {
  8. for(int i = 1; i <= n; i++)
  9. if(st[i] == 1) cout << "(";
  10. else cout << ")";
  11. cout << "\n";
  12. }
  13. int Valid(int top)
  14. {
  15. int i, nr1 = 0;
  16. for(i = 1; i <= top; i++)
  17. if(st[i] == 1) nr1++;
  18. if(nr1 < top - nr1) return 0;
  19. if(nr1 > n / 2) return 0;
  20. return 1;
  21. }
  22. void Back()
  23. {
  24. int cand, top;
  25. top = 1; st[top] = 0;
  26. while(top > 0)
  27. {
  28. cand = 0;
  29. while(cand == 0 && st[top] < 2)
  30. {
  31. st[top]++;
  32. cand = Valid(top);
  33. }
  34. if(cand == 0) top--;
  35. else if(top == n) Afisare();
  36. else st[++top] = 0;
  37. }
  38. }
  39. int main()
  40. {
  41. cin >> n;
  42. Back();
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement