#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int select = 1;
const double LING = 1E-6;
const int CONT = 4;
const int VOLUE = 24;
double number[CONT];
string expression[CONT];
bool m_judge = false; //判断是否有解。
int count = 0;
void Find(int n)
{
if (n == 1)
{
if ( fabs(number[0] - VOLUE) <= LING )
{
cout << expression[0] << "\t\t";
m_judge = true;
count ++;
if((count % 3)==0) //使输出时每行排列三个表达式
cout<<endl;
}
else
{ }
}
for(int i=0; i < n; i++)//查找
{
for (int j = i + 1; j < n; j++)//与其后面的查找进行计算
{
double a, b;
string expressiona, expressionb;
a = number[i];
b = number[j];
number[j] = number[n - 1]; //递归之后,n比以前小一位,所以可以不停向前赋值
expressiona = expression[i];
expressionb = expression[j];
expression[j] = expression[n - 1];
expression[i]= '('+ expressiona + '+' + expressionb + ')'; //加法不需要分顺序?
number[i] = a + b;
Find(n-1);
expression[i]='('+ expressiona+ '-' + expressionb + ')'; //减法应该分顺序,减数以及被减数
number[i] = a - b;
Find(n-1);
expression[i] = '('+expressionb + '-' + expressiona + ')';
number[i] = b -a;
Find(n-1);
expression[i]= '('+ expressiona +'*'+ expressionb+ ')'; //乘法不需要分顺序?
number[i]=a*b;
Find(n-1);
if (b != 0)
{
expression[i] ='('+expressiona+'/' + expressionb + ')'; //除法应该分顺序,除数以及被除数?
number[i] = a / b;
Find(n-1);
}
if (a != 0)
{
expression[i]='('+expressionb + '/'+ expressiona + ')';
number[i] = b / a;
Find(n-1);
}
number[i] =a; //防止以上几种都失败
number[j]=b;
expression[i] = expressiona;
expression[j] = expressionb;
}
}
}
int main()
{
while(select)
{
cout<<"请输入四个不大于13的正数:\n";
for (int i = 0; i < CONT; i++)
{
char ch[4];
cout<<"第"<<i+1<<"个数:";
cin >>number[i];
itoa(number[i],ch, 10); //itoa()函数的作用是把第一个参数(数值)传送(转换)到第二个参数(字符串)中去,第三个参数(int型)是该数值在字符串里以什么进制存放。
expression[i] = ch;
}
cout<<endl;
Find(CONT) ;
if(m_judge==true)
{
cout << "\n成功!" << endl;
cout<<"总共的计算方法共有: "<<count<<endl;
}
else
{
cout << "失败!" << endl;
}
}
system("pause");
return 0;
}
基于C++的简单24点小游戏
最新推荐文章于 2019-04-14 16:54:05 发布