递归求24点算法:
#include
#include
#include
using namespace std;
const double PRECISION = 1E-6;
const int COUNT = 4;
const int RESULT = 24;
double number[COUNT]; //这里一定要用double,看看第一题的答案就知道为什么了
string expression[COUNT]; //保存表达式
bool Test(int n)
{
//递归结束
if (n == 1)
{
if (fabs(number[0] - RESULT) < 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];