- cloudcone
-
不可能。下面是我写的算24点的程序,你把24改成10就可以了。
//file: Main.cpp
#include <iostream>
#include "CEquation.h"
#include <conio.h>
#define RESULT 24
using namespace std;
int main()
{
float oprands[4]; //输入的四个数字
float operators[4] = {PLUS, SUBSTRACT, MULTIPLY, DEVIDE};//四种运算符号
int nums[24][4]; //数字顺序
int ops[64][3]; //符号顺序
CEquation* equs[7680];//7680 = 24 * 64 * 5种运算表达式(后缀)
int count = 0;
cout<<"请输入四个数字,中间用空格隔开"<<endl;
cin>>oprands[0]>>oprands[1]>>oprands[2]>>oprands[3];//输入四个数字
count = 0;
int i = 0, j = 0, k = 0, m = 0;//循环变量
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
for (k = 0; k < 4; k++)
{
for (m = 0; m < 4; m++)
{
if (i != j && i != k && i != m && j != k && j != m && k != m)
{
nums[count][0] = i;
nums[count][1] = j;
nums[count][2] = k;
nums[count][3] = m;
count++;
}
}
}
}
}
count = 0;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
for (k = 0; k < 4; k++)
{
ops[count][0] = i;
ops[count][1] = j;
ops[count][2] = k;
count++;
}
}
}
count = 0;
for (i = 0; i < 24; i++)
{
for (j = 0; j < 64; j++)
{//添加五种可能的表达式
equs[count + 0] = new CEquation(oprands[nums[i][0]],
oprands[nums[i][1]],
operators[ops[j][0]],
oprands[nums[i][2]],
oprands[nums[i][3]],
operators[ops[j][1]],
operators[ops[j][2]]);
equs[count + 1] = new CEquation(oprands[nums[i][0]],
oprands[nums[i][1]],
operators[ops[j][0]],
oprands[nums[i][2]],
operators[ops[j][1]],
oprands[nums[i][3]],
operators[ops[j][2]]);
equs[count + 2] = new CEquation(oprands[nums[i][0]],
oprands[nums[i][1]],
operators[ops[j][0]],
oprands[nums[i][2]],
operators[ops[j][1]],
oprands[nums[i][3]],
operators[ops[j][2]]);
equs[count + 3] = new CEquation(oprands[nums[i][0]],
oprands[nums[i][1]],
oprands[nums[i][2]],
oprands[nums[i][3]],
operators[ops[j][0]],
operators[ops[j][1]],
operators[ops[j][2]]);
equs[count + 4] = new CEquation(oprands[nums[i][0]],
oprands[nums[i][1]],
oprands[nums[i][2]],
operators[ops[j][0]],
oprands[nums[i][3]],
operators[ops[j][1]],
operators[ops[j][2]]);
count += 5;
}
}
count = 0;
for (i = 0; i < 7680; i++)
{
if (equs[i] != NULL)
{
if (equs[i]->calValue() == RESULT)
{
equs[i]->printEquation();
count++;
}
}
}
if (count == 0)
cout<<"没能计算出"<<RESULT<<"点"<<endl;
getch();
return 0;
}
//file:CEquation.cpp
#include "CEquation.h"
#include "iostream.h"
#include <stack>
using namespace std;
CEquation::CEquation()
{
for (int i = 0; i < 7; i++)
{
s[i] = 0;
}
}
CEquation::CEquation(float s[])
{
for (int i = 0; i < 7; i++)
{
this->s[i] = s[i];
}
}
CEquation::CEquation(float x0, float x1, float x2,
float x3, float x4, float x5,
float x6)
{
s[0] = x0;
s[1] = x1;
s[2] = x2;
s[3] = x3;
s[4] = x4;
s[5] = x5;
s[6] = x6;
}
void CEquation::setEquation(float x0, float x1, float x2,
float x3, float x4, float x5,
float x6)
{
s[0] = x0;
s[1] = x1;
s[2] = x2;
s[3] = x3;
s[4] = x4;
s[5] = x5;
s[6] = x6;
}
float CEquation:: calValue()
{
stack<float> tmp;
float p, q;
for (int i = 0; i < 7; i++)
{
if (IS_OPERATOR(s[i]))//是运算符
{
switch(int(s[i]))
{
case PLUS:
p = tmp.top();
tmp.pop();
q = tmp.top();
tmp.pop();
tmp.push(q + p);
break;
case SUBSTRACT:
p = tmp.top();
tmp.pop();
q = tmp.top();
tmp.pop();
tmp.push(q - p);
break;
case MULTIPLY:
p = tmp.top();
tmp.pop();
q = tmp.top();
tmp.pop();
tmp.push(q * p);
break;
case DEVIDE:
p = tmp.top();
tmp.pop();
q = tmp.top();
tmp.pop();
if (p != 0)
tmp.push(q / p);
else
return -99999;
break;
}
}
else//是数字
{
tmp.push(s[i]);
}
}
return tmp.top();
}
void CEquation::printEquation()
{
for (int i = 0; i < 7; i++)
{
if (IS_OPERATOR(s[i]))
{
switch(int(s[i]))
{
case PLUS:
cout<<"+ ";
break;
case SUBSTRACT:
cout<<"- ";
break;
case MULTIPLY:
cout<<"* ";
break;
case DEVIDE:
cout<<"/ ";
break;
}
}
else
{
cout<<s[i]<<" ";
}
}
cout<<endl;
}
//file:CEquation.h
#ifndef __CEQUATION_H
#define __CEQUATION_H
#define PLUS 10000
#define SUBSTRACT 10001
#define MULTIPLY 10002
#define DEVIDE 10003
#define IS_OPERATOR(x) ((x) == PLUS || (x) == SUBSTRACT
|| (x) == MULTIPLY || (x) == DEVIDE)
class CEquation
{
private:
float s[7];//用来存储后缀表达式
public:
CEquation();
CEquation(float s[]);
CEquation(float, float, float, float, float, float, float);
void setEquation(float, float, float, float, float, float, float);
float calValue();
void printEquation();
};
#endif
- ardim
-
4/4=1
4*4=16
4+4=8
4-4=0
用这4个值中取两个(可重复取) 算出10...(= =好像不可能吧)
- meira
-
经过本人长达3分钟的思考,没做出来