1. Overview
Given four integers, each digit can only be used once. Any +-*/() is used to construct an expression, and the final result is 24, this is a common 24-point game. There are a lot of programs in this area, and they are generally used to solve the problem. This article introduces a typical 24-point algorithm, and provides two specific 24-point programs: process-oriented C implementation and Object-Oriented java implementation.
2,Basic Principles
The basic principle is to enumerate all the possible expressions of four integers and then evaluate the expressions.
Expression definition: expression = (expression | number) operator (expression | number)
Because the four operators +-*/can be used are all 2-element operators, this article only considers 2-element operators. The binary operator receives two parameters and outputs the calculation result. The output result is used for subsequent calculation.
As described above, the algorithm for constructing all possible expressions is as follows:
(1) Add four Integers to the array.
(2) sort two numbers in the array, with a total of P (4, 2. For each arrangement,
(2.1) +-*/each operator,
(2.1.1) Calculate the result based on the two numbers and operators listed here.
(2.1.2) change the table array: remove the two numbers in this arrangement from the array and put the result calculated by 2.1.1 into the array.
(2.1.3) Repeat Step 2 for the new array.
(2.1.4) restore an array: add the two numbers in the array and remove the result calculated by 2.1.1 from the array.
This is a recursive process. Step 2 is a recursive function. When there is only one number in the array, this is the final result of the expression, and Recursion ends.
In the program, you must pay attention to recursive on-site protection and recovery, that is, before and after recursive calls, the on-site status should be consistent. In the above algorithm, the recursive field is the exponential group, 2.1.2 changes the array for the next layer of recursive call, and 2.1.3 restores the array to ensure that the current recursive call gets the next correct arrangement.
Parentheses () only change the operator priority, that is, the order in which operators are calculated. Therefore, parentheses are not required in the preceding algorithms. Brackets must be considered only for output.
Process-oriented C implementation
This is the code of starfish, the former moderator of the csdn algorithm Forum. The program is very concise and 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;
&nbs
|