fork download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6. public static void main(String[] args) {
  7. /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Main. */
  8. Scanner in=new Scanner(System.in);
  9. int t=in.nextInt();
  10. for(int p=1;p<=t;p++)
  11. {
  12. int n=in.nextInt();
  13. char[] arr=new char[n*2];
  14. System.out.println("Test Case #"+p+":");
  15. f(arr,n+n,0,0,0);
  16. }
  17. }
  18. public static void f(char[] ar,int n,int open,int close,int idx){
  19. if(idx==n)
  20. {
  21. for(int i=0;i<n;i++)
  22. System.out.print(ar[i]);
  23. System.out.println();
  24. return;
  25. }
  26. if(open<n/2)
  27. {
  28. ar[idx]='{';
  29. f(ar,n,open+1,close,idx+1);
  30. }
  31. if(close<open)
  32. {
  33. ar[idx]='}';
  34. f(ar,n,open,close+1,idx+1);
  35. }
  36. }
  37. }
Success #stdin #stdout 0.15s 60852KB
stdin
1
4
stdout
Test Case #1:
{{{{}}}}
{{{}{}}}
{{{}}{}}
{{{}}}{}
{{}{{}}}
{{}{}{}}
{{}{}}{}
{{}}{{}}
{{}}{}{}
{}{{{}}}
{}{{}{}}
{}{{}}{}
{}{}{{}}
{}{}{}{}