Placing the Ops |
In an old brainteaser you are given a set of digits and a set of operators and asked to arrange the digits and the operators to form an expression that has a particular value. This problem is a variant of that brainteaser.
In this problem you will be presented with a sequence of no more than ten
digits (not necessarily unique) with an imbedded equal sign, and a collection
of at least one but no more than five integer operators
(from the set `+', `-', `*', and `/').
Your problem is to insert each of the operators between the correct pair of digits so the equation thus formed is arithmetically correct, assuming all operators have the same precedence, and that each side of the expression is evaluated strictly left to right. At least one digit will appear on each side of the equal sign.
For example, you might be given '957=52' and the operators `+' and
`*'. Arranging these in the form '9*5+7=52' makes the equation correct.
Or you might be given '123=456' and the operations `+', `+',
`*', and `*'. If you arrange these in the form '1*2+3=4-5+6'
you'll find each side of the equation has the value 5. As a final example
consider '135=642' and the operators `+', `+', `*'
and `*'. The arrangement '1+3*5=6+4*2' makes each side of the equation
have the value 20 (note the strict left-to-right evaluation order on each side
of the equation).
The division operator will yield only an integer result, and must obviously
never be used with a denominator of zero. No value in an expression will
require more than six decimal digits. Each operator must be used exactly once.
The order in which the digits appear, and the placement of the
equal sign cannot be altered.
957=52 +* 123=456 ++-* 135=642 ++** 8916=95 //+ 12=34 +- $
Case 1: 9*5+7=52 Case 2: 1*2+3=4-5+6 Case 3: 1+3*5=6+4*2 Case 4: 8+9/16=9/5 Case 5: NO SOLUTION