Problem #210
✰ - click to bookmark
★ - in your bookmarks
Tags:
puzzle
arithmetic
c-1
c-0
When I travel over the city by public transport, I get the ticket which usually contains 6-digit number and start
trying to build an arithmetic expression from this number according to following rules:
+, -, *, / could be inserted - or these digits could be just
"glued" together to form a larger value;100 as a result.For example, yesterday it was number 151374 which could be solved in several ways:
1 + (5 + 1 + 3) * (7 + 4) = 100
(1 * 5) + (13 * 7) + 4 = 100
If ever I could not come up with solution I wonder, whether it is really not possible or I am just not smart enough.
So let us write the program to help checking this out. We will try to solve this for different target values,
not necessarily for 100.
Few restrictions:
5 operators should have 5 opening and 5 closing brackets;Input data will contain amount of test cases in the first line.
Next lines will contain test cases - the ticket number and the "target" value (separated by equals sign).
Answer should contain proposed expression for each test case. If there is no solution - output zero. Separate
answers with spaces (but do not use spaces inside expression).
Example:
input data:
5
151374=100
256128=128
577976=154
942511=175
767494=168
answer: (1+(((5+1)+3)*(7+4))) (256-128) ((((5*7)*7)-97)+6) 0 (((7-6)*74)+94)