CS331 Advanced Data Structures – Spring 2016 50 points Due: Feb. 15

advertisement
CS331 Advanced Data Structures – Spring 2016
Program 1 – Challenge 24
50 points
Due: Feb. 15
The Problem : Challenge 24 is a popular game in elementary schools around the country. Students are given 4 digits in the range 1 to 9 and asked to combine them using the four standard
arithmetic operators so that the expression evaluates to 24. So for example, with the number
5, 3, 9 and 6 you can get 24 by the expressions 5 × 6 − 9 + 3 or (5 + 3) ∗ (9 − 6). Notice
that the values can be in any order in the expression, but cannot be repeated, unless they are
repeated in the original set. We’ll expand on the original Challenge 24 by asking for a list of
all possible positive values that can be obtained using the original 4 numbers.
Input : Input will consist of 4 integers in the range 1 to 9. Repeat values are possible and the
values can be given in any order
Output : Output all possible positive values that can be achieved with these four numbers and
the four basic arithmetic operators. These values should be listed in ascending order. Note
that division should only be done when the divisor evenly divides the dividend (i.e., there
should be no remainder).
Example : On input
5 3 9 6
your code should output
1 2 3 4 5 6
31 32 33 35
67 69 72 76
144 147 153
297 360 405
7 8 9 10 11
36 38 39 40
77 78 81 86
157 165 167
432 810
12 13 14 15
41 42 43 45
87 90 96 99
177 180 189
16 17 18 19
46 47 48 50
102 105 108
192 207 225
21 22 23 24
52 54 55 56
117 120 126
243 252 255
25 26 27 28
57 60 62 63
129 132 135
267 273 285
30
66
141
288
On input
3 3 3 3
your code should output
1 2 3 4 5 6 7 8 9 10 12 15 18 21 24 27 30 36 54 81
Algorithm : You should use two different backtracking algorithms for this assignment. The first
should determine all possible values achievable by a set of 4 numbers in a fixed order (i.e.,
without rearranging them). The second should determine all possible orderings of the 4
numbers given. Combining these two together will solve the problem. You should work on
the first algorithm initially (and will get partial credit if that’s all you turn in), and then add
in the second algorithm afterwards.
You may work in groups for this project.
Download