# Recursion-24 o'clock

Source: Internet
Author: User

Recently, I wrote d a 24-point algorithm. However, at the same time, I can input a set of numbers and output an arithmetic formula according to the required answers. This algorithm mainly uses recursion and layer-by-layer computing.

Example: (3*3) + 4)/3) * 6) = 26;

The following code is used:

CPP file:

# Include "alg_ans.h"
Void main ()
{
Vector <double> input;
String exp;
Int con_record = 0;
Int ans = 24;
Cout <"How many number do you want to input..." <Endl;
Cin> con_record;
Cout <"what is the answer do you want to get..." <Endl;
Cin> ans;
Cout <"input" <con_record <"number..." <Endl;
For (INT I = 0; I <con_record; I ++)
{
Int X;
Cin> X;
Input. push_back (X );
}
Rotate (input, exp, ANS, con_record );
}

Header file ALG _ ans:

# Include <iostream>
# Include <vector>
# Include <string>
# Include <cmath>
Using namespace STD;
Const Double Precision = 1e-6; // precision, to prevent errors due to precision issues during Division operations

/* Idea: A, B, C, D => A, B, (C + *-/d) => A, B, C =>, C => C = ans? Then return computing layer by layer
*/
Bool compute_ans (vector <double> Number, string exp, int ans, int con_record = 0)
{
If (number. Size () = 1)
{
If (FABS (number -ans) <precision)
{
Cout <"success..." <Endl;
Cout <exp <"=" <ans <Endl;
Return true;
}
Else
{
Return false;
}
}
Else
{
String exp1, exp2;
Double A, B, C;
Char ch_a, ch_ B;
Exp1 = exp;

A = number [number. Size ()-1];
Ch_a = a + 48;
Number. pop_back ();
B = number [number. Size ()-1];
Ch_ B = B + 48;
Exp2 = ch_ B;
Number. pop_back ();
C = A + B;
Number. push_back (C );

If (con_record> 0)
{
Exp1 = ch_a;
}
Exp = '(' + exp1 + '+ exp2 + ')';
If (compute_ans (number, exp, ANS) return true;

Number. pop_back ();
C = A * B;
Number. push_back (C );
Exp = '(' + exp1 + '*' + exp2 + ')';
If (compute_ans (number, exp, ANS) return true;

Number. pop_back ();
{
If (A> B)
{
C = A-B;
Number. push_back (C );
Exp = '(' + exp1 + '-' + exp2 + ')';
}
Else
{
C = B-;
Number. push_back (C );
Exp = '(' + exp2 + '-' + exp1 + ')';
}
}
If (compute_ans (number, exp, ANS) return true;

Number. pop_back ();
If (FABS (B)> precision)
{
C = A/B;
Number. push_back (C );
Exp = '(' + exp1 + '/' + exp2 + ')';
If (compute_ans (number, exp, ANS) return true;
}

Number. pop_back ();
If (FABS (a)> precision)
{
C = B/;
Number. push_back (C );
Exp = '(' + exp2 + '/' + exp1 + ')';
If (compute_ans (number, exp, ANS) return true;
}
}
Return false;
}

/* The insert_char function is used to insert characters at a certain position */
Void insert_char (char C1, string & STR, int I)
{
If (I = 0)
{
STR = C1 + STR;
}
Else
{
String temp_str = Str. substr (I );
Str. Erase (I, str. Size ()-I );
STR = STR + C1 + temp_str;
}
}
/* The rotate function is used to arrange all input numbers in a non-recursive full arrangement mode, mainly because
The recursive method has been used in the calculation formula. Therefore, a non-recursive function can be used to call the recursive function.
This reduces the amount of stack space occupied.
The method for fully arranging input numbers is as follows: 1 first, the subscript is fully arranged.
2. Then, the numbers are sorted by the subscript to obtain the full arrangement of a series.
*/
Void rotate (vector <double> Number, string exp, int ans, int con_record)
{
Int COUNT = number. Size ();

Vector <string> temp;
Vector <string> store;

Store. push_back ("0 ");

String t_str;
Char ch_ I;

For (INT I = 1; I <count; I ++) // use the recursive method to insert characters at intervals to obtain a full array.
{
For (Int J = 0; j <store. Size (); j ++)
{
T_str = store [J];
For (int t = 0; t <= store [J]. Size (); t ++)
{
Ch_ I = I + 48;
Insert_char (ch_ I, t_str, t );
Temp. push_back (t_str );
T_str = store [J];
}
}
While (! Store. Empty ())
{
Store. pop_back ();
}
Store = temp;
While (! Temp. Empty ())
{
Temp. pop_back ();
}
}
// The subscript sorting is obtained.

Vector <double> sort_number;
Int t_int;
Bool test = false;
For (I = 0; I <store. Size (); I ++)
{
For (Int J = 0; j <number. Size (); j ++)
{
T_int = store [I] [J];
T_int = T_int-48;
Sort_number.push_back (number [t_int]);
}
If (compute_ans (sort_number, exp, ANS, con_record ))
{
Test = true;
Break;
}
While (! Sort_number.empty ())
{
Sort_number.pop_back ();
}

}
If (! Test)
Cout <"fail to compute..." <Endl;

}

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.