gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
expanded sample size, first version
[mjc2wsl.git] / samples / alpha-mj-exp / Parentheses.mj
1 program Parentheses
2 {
3 // generate all possibilities of n pairs of parentheses
4 // so that they are valid (open - closed)
6 void parentheses(char[] solution, int pos, int left, int right, int num)
7 int i;
8 {
9 if (right == num) {
10 i = 0;
11 while (i < len(solution)) {
12 print(solution[i]);
13 print(' ');
14 i++;
15 }
16 print('\n');
17 }
18 if (left < num) {
19 solution[pos] = '(';
20 parentheses(solution, pos+1, left+1, right, num);
21 }
22 if (left > right) {
23 solution[pos] = ')';
24 parentheses(solution, pos+1, left, right+1, num);
25 }
26 }
28 void pars(char[] sln, int num) {
29 if (num > 0) {
30 parentheses(sln, 0, 0, 0, num);
31 }
32 }
34 void main()
35 int i, dimension;
36 char[] solution;
37 {
38 print('d');
39 print('?');
40 read(dimension);
41 solution = new char[2*dimension];
42 pars(solution, dimension);
43 }
44 }
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner