Problem H
Game Show Math
Input: standard input
Output: standard output
Time Limit: 15 seconds
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:
o the numbers in the expression must appear in the same order as they appear in the input file
o 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.
o 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/5*1*2=4
题意:给定n个数字。和一个answer。要求在n个数字中插入“+-*/“ 使得式子成立。。
题目给了几个限定条件:1、数字位置不能变,且运算符优先级与先后顺序有关(和+-*/无关)。。
2、运算过程结果必须一直保持在[-32000,32000]之间。
3、‘/’号只能在整除的情况下才能使用。。
思路:直接暴力时间复杂度O(4 ^ n)。这题最多100。。果断超时的节奏。。于是乎用记忆化搜索。
开一个二维数组vis[i][j].i表示加到第几个,j表示当前结果。。如果重复状态直接return。
这里有一个要注意的地方。就是j表示的当前结果可能为负数。但是负数最多为-32000。。所以我们可以把结果都加上32000.这样就没有问题了。
代码:
#include <stdio.h>
#include <string.h> int vis[105][66666];
int t, n, num[105], ans, judge;
char out[105];
void dfs(int start, int sum) {
if (start == n) {
if (sum == ans)
judge = 1;
return;
}
if (sum + num[start] <= 32000 && !vis[start][sum + num[start] + 32000]) {
out[start] = '+';
vis[start][sum + num[start] + 32000] = 1;
dfs(start + 1, sum + num[start]);
if (judge) return;
}
if (sum - num[start] >= -32000 && !vis[start][sum - num[start] + 32000]) {
out[start] = '-';
vis[start][sum - num[start] + 32000] = 1;
dfs(start + 1, sum - num[start]);
if (judge) return;
}
if (sum * num[start] >= -32000 && sum * num[start] <= 32000 && !vis[start][sum * num[start] + 32000]) {
out[start] = '*';
vis[start][sum * num[start] + 32000] = 1;
dfs(start + 1, sum * num[start]);
if (judge) return;
}
if (!(sum % num[start])) {
if (sum / num[start] >= -32000 && sum / num[start] <= 32000 && !vis[start][sum / num[start] + 32000]) {
out[start] = '/';
vis[start][sum / num[start] + 32000] = 1;
dfs(start + 1, sum / num[start]);
if (judge) return;
}
}
}
int main() {
scanf("%d", &t);
while (t --) {
judge = 0;
memset(vis, 0, sizeof(vis));
scanf("%d", &n);
for (int i = 0; i < n; i ++)
scanf("%d", &num[i]);
scanf("%d", &ans);
vis[0][num[0] + 32000] = 1;
dfs(1, num[0]);
if (judge) {
for (int i = 0; i < n; i ++) {
if (i != 0)
printf("%c%d", out[i], num[i]);
else
printf("%d", num[i]);
}
printf("=%d\n", ans);
}
else printf("NO EXPRESSION\n");
}
return 0;
}