P1236 24 Game
Description
Decades ago, a number game became popular worldwide and is still enjoyed today. In China, we call this game "24 Game". As a player, you are given 4 natural numbers between $1$ and $9$ as operands. Your task is to perform arithmetic operations on these 4 operands so that the result equals $24$.
You may use only the operations $\verb!+!, \verb!-!, \verb!*!,$ and $\verb!/!$. You may also use $\verb!()!$ to change the order of operations. Note: all intermediate results must be integers, so some divisions are not allowed (for example, $(2\times 2)/4$ is valid, while $2\times (2/4)$ is invalid). Here is a concrete example of the game:
If the 4 operands are $1$, $2$, $3$, and $7$, then one possible answer is $1+2+3\times 7=24$.
Input Format
A single line containing four natural numbers between $1$ and $9$ (inclusive).
Output Format
If a solution exists, output any one solution. The output consists of three lines, each describing one computation step.
- The first line is two input numbers, an operator, and the result after the operation.
- The second line is the result from the first line combined with one remaining input number, an operator, and the new result, or the result of applying an operator to the other two input numbers.
- The third line combines the previous result, the second line’s result or the remaining number, an operator, and $\verb!=24!$. If two operands are different, output the larger one first.
If there is no solution, output `No answer!`.
If multiple valid solutions exist, output any one of them.
Note: all operation results must be positive integers.
Explanation/Hint
Thanks to chenyy for the special judge.
---
$\text{upd 2022.8.1}$: Added a new set of hack testdata.
Translated by ChatGPT 5