fork download
  1. // 4.7
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. void solve(int open, int close, int n, string s) {
  7. if (s.length() == 2 * n) {
  8. cout<< s<< endl;
  9. return;
  10. }
  11.  
  12. if (open < n)
  13. solve(open + 1, close, n, s + '(');
  14.  
  15. if (close < open)
  16. solve(open, close + 1, n, s + ')');
  17. }
  18.  
  19. int main() {
  20. int n;
  21. cin >> n;
  22. solve(0, 0, n, "");
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0.01s 5284KB
stdin
5
stdout
((((()))))
(((()())))
(((())()))
(((()))())
(((())))()
((()(())))
((()()()))
((()())())
((()()))()
((())(()))
((())()())
((())())()
((()))(())
((()))()()
(()((())))
(()(()()))
(()(())())
(()(()))()
(()()(()))
(()()()())
(()()())()
(()())(())
(()())()()
(())((()))
(())(()())
(())(())()
(())()(())
(())()()()
()(((())))
()((()()))
()((())())
()((()))()
()(()(()))
()(()()())
()(()())()
()(())(())
()(())()()
()()((()))
()()(()())
()()(())()
()()()(())
()()()()()