【题目描述】
给出a,b,c的组合2,3,4,可能通过
((a+b)*c)
求出14吗?
【解题思路】
参考官方解题报告
const int MAXVAR = 5;
typedef struct expr {
char op;
int * v;
expr * izq;
expr * der;
} expr;
int values[MAXVAR];
string expression;
expr* parseExpression(const string expression, int &pos, int &nvar)
{
expr * ret = NULL;
if (expression[pos] == '(') {
ret = new expr;
pos++;
ret->izq = parseExpression(expression, pos, nvar);
ret->op = expression[pos];
pos++;
ret->der = parseExpression(expression, pos, nvar);
pos++;
} else if (islower(expression[pos])) {
ret = new expr;
ret->v = values + nvar;
ret->izq = ret->der = NULL;
pos++;
nvar++;
}
return ret;
}
int evalExpression(expr * e)
{
int res = -1;
if ((e->izq == NULL) && (e->der == NULL)) {
res = *(e->v);
} else {
int v1 = evalExpression(e->izq);
int v2 = evalExpression(e->der);
switch (e->op) {
case '+': res = v1 + v2; break;
case '-': res = v1 - v2; break;
case '*': res = v1 * v2; break;
}
}
return res;
}
void freeExpr(expr* &e)
{
if ( e->izq != NULL ) freeExpr(e->izq);
if ( e->der != NULL ) freeExpr(e->der);
delete e;
}
void showExpression(expr* e)
{
if ((e->izq == NULL) && (e->der == NULL)) {
cout << "V";
} else {
cout << '(';
showExpression(e->izq);
cout << e->op;
showExpression( e->der);
cout << ')';
}
}
int main()
{
expr* e;
int i;
int pos = 0, nvar = 0;
int n, result, act;
bool possible;
while (1) {
cin >> n;
for (i = 0; i < n; ++i) {
cin >> values[i];
}
sort(values, values+n);
cin >> result;
if ((n == 0) && (result == 0))
break;
cin >> expression;
pos = nvar = 0;
e = parseExpression(expression,pos,nvar);
possible = false;
do {
act = evalExpression(e);
if (act == result) {
possible = true;
}
} while(!possible && next_permutation(values,values+n));
if (possible) cout << "YES" << endl;
else cout << "NO" << endl;
freeExpr(e);
}
return 0;
}