如何验证5个给定数字通过+-*运算(同优先级左到右)得到23?
Alright, let's tackle how to figure out if 5 given numbers can be combined with +, -, * (evaluated strictly left-to-right, no standard operator precedence) to hit the result 23. I'll break this down into actionable steps, plus share a practical way to automate the check.
Core Approach
Since both the order of numbers and the choice of operators affect the final result, we need to exhaust all possible combinations and evaluate each one:
1. Generate all unique permutations of the 5 numbers
- Different number orders will lead to different results (e.g.,
1+2*3left-to-right equals9, while2+1*3left-to-right equals6), so we need to consider every possible arrangement. - If there are duplicate numbers in the input, we can skip duplicate permutations to save computation time (like with five
1s, there's only one unique permutation). - For distinct numbers like
1,2,3,4,5, there are5! = 120total permutations.
2. Generate all possible operator combinations
- Between 5 numbers, there are 4 operator slots. Each slot can be
+,-, or*, so that's3^4 = 81total operator combinations. - Examples of these combinations include
[+, *, -, +]or[*, *, *, *].
3. Evaluate each (permutation + operator combo) pair left-to-right
- For each pair, calculate the result step by step from left to right—do not apply standard math precedence (so multiplication doesn't get priority over addition/subtraction).
- Example calculation:
- Permutation:
[3, 4, 5, 2, 1] - Operators:
[*, +, -, +] - Step 1:
3 * 4 = 12 - Step 2:
12 + 5 = 17 - Step 3:
17 - 2 = 15 - Step 4:
15 + 1 = 16→ Not 23, so this combo doesn't work.
- Permutation:
4. Check for a valid result
- If any of the evaluated pairs equals 23, we can conclude it's possible. If none do after checking all combinations, it's impossible.
- For the example of five
1s: No matter the operator combo, the result will range from-3to5—way off from 23, so it's impossible.
Practical Implementation
Manual calculation for all combinations is tedious, so here's a simple Python script to automate the process:
import itertools def can_reach_23(numbers): # Get unique permutations to avoid redundant calculations unique_perms = set(itertools.permutations(numbers)) # Generate all 4-operator combinations operator_combos = itertools.product(['+', '-', '*'], repeat=4) for perm in unique_perms: for ops in operator_combos: current = perm[0] # Calculate left to right for i in range(4): if ops[i] == '+': current += perm[i+1] elif ops[i] == '-': current -= perm[i+1] elif ops[i] == '*': current *= perm[i+1] if current == 23: # Build the expression string for clarity expr = str(perm[0]) for j in range(4): expr += f"{ops[j]}{perm[j+1]}" print(f"Valid combination found: {expr} = 23") return True print("No valid combination exists.") return False # Test the examples can_reach_23([1, 1, 1, 1, 1]) # Output: No valid combination exists. / Returns False can_reach_23([1, 2, 3, 4, 5]) # Output: Prints a valid combination + Returns True
内容的提问来源于stack exchange,提问作者Kamran Poladov
相关产品推荐
相关产品推荐

