1. Overview
Given 4 integers, each of these numbers can only be used once, and arbitrarily use +-*/() to construct an expression that finally results to 24, which is a common 24-point game. The procedures in this area are very numerous and are generally solved by poor lifting. This paper introduces a typical 24-point program algorithm, and gives two detailed procedures to calculate 24 points: One is the process-oriented C implementation, and the other is the object-oriented Java implementation.
2. Basic Principles
The basic principle is to exhaustive all possible expressions for 4 integers, and then evaluate the expression.
Definition of expressions: expression = (expression|number) operator (Expression|number)
Because the 4 operators that can be used +-*/are all 2-dollar operators, only the 2-dollar operator is considered in this article. The 2-tuple operator receives two parameters, outputs the result of the calculation, and the result of the output is calculated.
As described above, the algorithm constructs all possible expressions such as the following:
(1) Put 4 integers in the array
(2) A permutation of two numbers in an array, together with a P (4,2) arrangement. For each arrangement,
(2.1) to +-*/each operator,
(2.1.1) Calculates the result based on the two numbers and operators in this arrangement
(2.1.2) Array: Remove the two numbers from the array and put the results of the 2.1.1 calculation into the array
(2.1.3) to the new array, repeat steps 2
(2.1.4) to restore an array: Adds the array of two numbers in this arrangement, removing the result of the 2.1.1 calculation from the array
This can be seen as a recursive process. Step 2 is the recursive function. When there is only one number left in the array, this is the final result of the expression, at which point the recursion ends.
In the program, it is important to pay attention to recursive field protection and recovery, that is, the recursive call before and after the field status should be consistent. In the above algorithm, the recursive field is the exponential group, 2.1.2 changes the array for the next level of recursive invocation, 2.1.3 restores the array to ensure that the current recursive call obtains the next correct arrangement.
The function of parentheses () is simply to change the precedence of operators, which is the order in which operators are evaluated. Therefore, in the above algorithm, there is no need to consider parentheses. Parentheses are only required to be considered when outputting.
3, process-oriented C implementation
This is the CSDN algorithm forum before the moderator Starfish Code, the program is concise, exquisite:
#include
#include
#include
using namespace Std;
Const double PRECISION = 1E-6;
const int count_of_number = 4;
const int number_to_be_cal = 24;
Double Number[count_of_number];
String Expression[count_of_number];
BOOL Search (int n)
{
if (n = = 1) {
if (Fabs (number[0]-number_to_be_cal) < PRECISION) {
cout << expression[0] << Endl;
return true;
} else {
return false;
}
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < N; j + +) {
Double A, B;
String Expa, EXPB;
A = Number[i];
b = Number[j];
NUMBER[J] = number[n-1];
EXPA = Expression[i];
EXPB = Expression[j];
EXPRESSION[J] = expression[n-1];
Expression[i] = ' (' + expa + ' + ' + EXPB + ') ';
Number[i] = a + b;
if (Search (n-1)) return true;
Expression[i] = ' (' + expa + '-' + EXPB + ') ';
Number[i] = a-B;
if (Search (n-1)) return true;
Expression[i] = ' (' + EXPB + '-' + Expa + ') ';
Number[i] = b-a;
if (Search (n-1)) return true;
Expression[i] = ' (' + expa + ' * ' + EXPB + ') ';
Number[i] = a * b;
if (Search (n-1)) return true;
if (b! = 0) {
Expression[i] = ' (' + expa + '/' + EXPB + ') ';
Number[i] = A/b;
if (Search (n-1)) return true;
}
if (A! = 0) {
Expression[i] = ' (' + EXPB + '/' + Expa + ') ';
Number[i] = b/a;
if (Search (n-1)) return true;
}
Number[i] = A;
NUMBER[J] = b;
Expression[i] = EXPA;
EXPRESSION[J] = EXPB;
}
}
return false;
}
void Main ()
{
for (int i = 0; i < Count_of_number; i++) {
Char buffer[20];
int x;
CIN >> X;
Number[i] = x;
Itoa (x, buffer, 10);
Expression[i] = buffer;
}
if (Search (Count_of_number)) {
cout << "Success" << Endl;
} else {
cout << "Fail." << Endl;
}
}
It can be compiled with any C + + compiler.
The algorithm of this program and 2, the basic principle of the algorithm is basically the same. where bool Search (int n) is a recursive function, double number[] is an array. The more important parts of the program are explained in the following examples:
(1) string expression[] stores the expression generated at each step, which is used in the final output. Expression[], similar to number[, is also the site of recursive invocation, which must be changed before the next level of recursive invocation and resumed after the next level of recursive invocation.
(2) number[] array length is only 4. In search (), after removing two numbers each time, use local variable A, B to save the two numbers, add the result of the array at the same time, and adjust the array so that the valid numbers are arranged in front of the array. After the next level of recursive invocation, the entire array is recovered using local variable a, B. The treatment of expression[] is similar to that of number[].
(3) because the + * satisfies the exchange rate and-/is not satisfied, so in the program, from the array to generate two number of permutations,
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < N; j + +) {
The Inner Loop j is from I+1 to N, not from 0->n, because the order of two numbers is irrelevant for the exchange rate. Of course, the inside of the loop-/special treatment, calculated the A-B b-a A/a b/a four cases.
(4) This procedure only find the first solution. When the first solution is obtained, the result is returned by the layer return true and the program ends.
(5) The solution is solved by double, and the precision is defined to infer whether it is 24. Consider the expression "5-1/5" to know the reason for doing so.
(6) When outputting, parentheses are added to each expression.
24-Point Classic algorithm