I found a lot of post quite similar (referring to the Coin changing Problem) but only using the sum operator. Now imagine you can add, subtract, multiply a
Since you only have binary operations, you can model any of the calculations as a binary tree where the leaves are numbers and all other nodes are representing operations, e.g. for your first two examples:
+ -
/ \ / \
9 + * +
/ \ /| / \
1 + 2 9 - 1
/ \ / \
4 2 5 4
So your algorithm would need the following parts:
.
N O O O ...
/ \ / \ / \
N N O N N O
/ \ / \
N N N N
.
O : + + ... - ...
/ \ / \ / \ / \
N N 1 5 1 2 1 5
Happy programming! :-)