Advertisement
cat_baxter

Printing braces

Jun 21st, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. unsigned long pairs_count;
  5. unsigned long *brackets;
  6. unsigned long found = 0;
  7.  
  8. void print_result()
  9. {
  10.     unsigned long i, j;
  11.  
  12.     for (i = 0; i < pairs_count; i++) {
  13.     printf("(");
  14.  
  15.     for (j = 0; j < pairs_count; j++)
  16.         if (brackets[j] == i)
  17.         printf(")");
  18.     }
  19.  
  20.     printf("n");
  21.     found++;
  22. }
  23.  
  24. void step(unsigned long start_val, unsigned long pos)
  25. {
  26.     unsigned long k, i;
  27.  
  28.     if (start_val > pos)
  29.     k = start_val;
  30.     else
  31.     k = pos;
  32.  
  33.     for (i = k; i < pairs_count; i++) {
  34.     brackets[pos] = i;
  35.  
  36.     if (pos == pairs_count - 1)
  37.         print_result();
  38.     else
  39.         step(i, pos + 1);
  40.     }
  41. }
  42.  
  43. int main()
  44. {
  45.  
  46.     scanf("%d", &pairs_count);
  47.  
  48.     brackets = new unsigned long[pairs_count];
  49.  
  50.     if (!brackets) {
  51.     printf("Unsufficient memory.n");
  52.     return (0);
  53.     }
  54.  
  55.     step(0, 0);
  56.  
  57.     delete brackets;
  58.  
  59.     printf("Found: %d.n", found);
  60.  
  61.     return (0);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement