如何用C语言找出运算符组合使等式8?7?6?5?4?3?2?1=36成立
Hey there! Let's work through this problem together. First off, I noticed a small mistake in your initial calculation of possible combinations: since there are 7 operator positions and 4 choices for each, it's 4^7 = 16384 total combinations, not 4*7=28. That's a big difference, but don't worry—modern computers can handle this number of iterations easily.
Your idea of starting with all + and replacing operators is a form of brute-force enumeration, which is totally valid here. The key missing piece is probably handling operator precedence (multiplication and division need to be calculated before addition and subtraction), otherwise your results will be wrong.
Here's a practical C program that enumerates all possible operator combinations, correctly evaluates the expression with precedence, and prints any valid equations that equal 36:
#include <stdio.h> // Calculates the result of the expression with proper operator precedence int calculate_expression(int nums[], char ops[], int num_count) { int total = nums[0]; int prev_value = nums[0]; // Tracks the value to use for pending * or / operations for (int i = 0; i < num_count - 1; i++) { int current_num = nums[i + 1]; switch (ops[i]) { case '+': total += current_num; prev_value = current_num; break; case '-': total -= current_num; prev_value = -current_num; break; case '*': // Adjust total by removing the previous value, then add the product total = total - prev_value + prev_value * current_num; prev_value = prev_value * current_num; break; case '/': // Assuming integer division (since we're targeting an integer result of 36) total = total - prev_value + prev_value / current_num; prev_value = prev_value / current_num; break; } } return total; } int main() { int numbers[] = {8, 7, 6, 5, 4, 3, 2, 1}; const char operators[] = "+-*/"; char current_ops[7]; // Stores the 7 operators for each combination // Iterate through all 4^7 = 16384 possible combinations for (int i = 0; i < 16384; i++) { int temp = i; // Convert the integer to a 7-digit base-4 number to pick operators for (int j = 6; j >= 0; j--) { int op_index = temp % 4; current_ops[j] = operators[op_index]; temp /= 4; } // Check if this combination gives us 36 int result = calculate_expression(numbers, current_ops, 8); if (result == 36) { // Print the valid equation printf("%d", numbers[0]); for (int k = 0; k < 7; k++) { printf("%c%d", current_ops[k], numbers[k + 1]); } printf(" = 36\n"); } } return 0; }
How this works:
- Enumeration: We use an integer from 0 to 16383 to represent all possible operator combinations. Each digit in the base-4 version of this integer maps to an operator (0=+, 1=-, 2=*, 3=/).
- Precedence Handling: The
calculate_expressionfunction uses aprev_valuevariable to keep track of values that need to be multiplied or divided before adding/subtracting. This avoids the need for a stack and keeps the logic simple. - Validation: For each combination, we compute the result and print the equation if it equals 36.
When you run this program, it will output all valid operator combinations that satisfy the equation. Give it a try!
内容的提问来源于stack exchange,提问作者Dum Dum

