擅长:python、mysql、java
<p>我希望这将帮助您开始:</p>
<pre class="lang-py prettyprint-override"><code>operators = ['-', '+', '*', '/']
operands = ['a', 'b', 'c']
# find out all possible combination of operators first. So if you have 3 operands, that would be all permutations of the operators, taken 2 at a time. Also append the same expression operator combinations to the list
from itertools import permutations
operator_combinations = list(permutations(operators, len(operands)-1))
operator_combinations.extend([op]*(len(operands)-1) for op in operators)
# create a list for each possible expression, appending it with an operand and then an operator and so on, finishing off with an operand.
exp = []
for symbols in operator_combinations:
temp = []
for o,s in zip(operands, symbols):
temp.extend([o,s])
temp.append(operands[-1])
exp.append(temp)
for ans in exp:
print(''.join(ans))
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>a-b+c
a-b*c
a-b/c
a+b-c
a+b*c
a+b/c
a*b-c
a*b+c
a*b/c
a/b-c
a/b+c
a/b*c
a-b-c
a+b+c
a*b*c
a/b/c
</code></pre>