A game show in Britain has a segment where it gives its contestants a sequence of positive numbers and a target number. The contestant must make a mathematical expression using all of the numbers in the sequence and only the operators: +, -, * and /. Each number in the sequence must be used exactly once, but each operator may be used zero to many times. The expression should be read from left to right, without regard for order of operations, to calculate the target number. It is possible that no expression can generate the target number. It is possible that many expressions can generate the target number.
There are three restrictions on the composition of the mathematical expression:
• the numbers in the expression must appear in the same order as they appear in the input file
• Since the target will always be an integer value (a positive number), you are only allowed to use / in the expression when the result will give a remainder of zero.
• You are only allowed to use an operator in the expression, if its result after applying that operator is an integer from (-32000 . . . +32000).
Input
The input file describes multiple test cases. The first line contains the number of test cases n.
Each subsequent line contains the number of positive numbers in the sequence p, followed by p positive numbers, followed by the target number. Note that 0 < p ≤ 100. There may be duplicate numbers in the sequence. But all the numbers are less than 32000.
Output
The output file should contain an expression, including all k numbers and k−1 operators plus the equals sign and the target. Do not include spaces in your expression. Remember that order of operations does not apply here. If there is no expression possible output ‘NO EXPRESSION’ (without the quotes). If more than one expression is possible, any one of them will do.
Sample Input
3
3 5 7 4 3
2 1 1 2000
5 12 2 5 1 2 4
Sample Output
5+7/4=3
NO EXPRESSION
12-2/512=4
问题链接:UVA10400 Game Show Math
问题简述:(略)
问题分析:一道经典的DFS题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA10400 Game Show Math */
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 32000;
const char OP[] = {'+', '-', '*', '/'};
const int L = sizeof(OP) / sizeof(OP[0]);
const int N = 100;
int n, a[N], target, vis[N][MAXN * 2], flag;
char op[N];
void dfs(int k, int sum)
{
if(k == n - 1) {
if(sum == target) flag = 1;
} else {
int t;
for(int i = 0; i < L; i++) {
if((op[k] = OP[i]) == '+') {
if(abs(t = sum + a[k + 1]) <= MAXN)
if(vis[k][t + MAXN] == 0)
dfs(k + 1, t);
} else if((op[k] = OP[i]) == '-') {
if(abs(t = sum - a[k + 1]) <= MAXN)
if(vis[k][t + MAXN] == 0)
dfs(k + 1, t);
} else if((op[k] = OP[i]) == '*') {
if(abs(t = sum * a[k + 1]) <= MAXN)
if(vis[k][t + MAXN] == 0)
dfs(k + 1, t);
} else if((op[k] = OP[i]) == '/') {
if(sum % a[k + 1] == 0)
if(abs(t = sum / a[k + 1]) <= MAXN)
if(vis[k][t + MAXN] == 0)
dfs(k + 1, t);
}
if(flag) return;
else if(abs(t) <= MAXN && vis[k][t + MAXN] == 0)
vis[k][t + MAXN] = 1;
}
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
scanf("%d", &target);
memset(vis, 0, sizeof(vis));
flag = 0;
dfs(0, a[0]);
if(flag) {
for(int i = 0; i < n; i++) {
if(i == n - 1)
printf("%d=%d\n", a[i], target);
else
printf ("%d%c", a[i], op[i]);
}
} else
printf ("NO EXPRESSION\n");
}
return 0;
}