uva 10400 Game Show Math(深搜 )

uva 10400 Game Show Math

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 casesn.

Each subsequent line contains the number of positive numbers in the sequence p, followed by p positive numbers, followed by the target number. Note that0 < p 100. There may be duplicate numbers in the sequence. But all the numbers are less than32000.

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

33 5 7 4 32 1 1 20005 12 2 5 1 2 4

Sample Output5+7/4=3 NO EXPRESSION

12-2/5*1*2=4

题目大意:给出一些数字和一个目标数字,任意使用+,,-,*,/四个符号计算(在这里四个符号的优先级都是相同的),是这些数字表达式的结果为目标数字。

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>using namespace std;int num[105], vis[105][70000], ans, cnt, flag, cnt2, n;char str[10005];int DFS(int d, int result, char order) {if (flag) return 1;if (order != ' ') {str[d – 2] = order;}if (d == n) {if (result == ans) {flag = 1;return 1;}return 0;}if (result < -32000 || result > 32000) {return 0;}if (vis[d][result]) return 0; //记录前n个数求出的result,若已记录,代表当前result再往后递归也求不出解, 直接结束vis[d][result] = 1;DFS(d + 1, result + num[d], '+');DFS(d + 1, result – num[d], '-');DFS(d + 1, result * num[d], '*');DFS(d + 1, result / num[d], '/');}int main() {int T;scanf("%d", &T);while (T–) {memset(str, 0, sizeof(str));memset(vis, 0, sizeof(vis));scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &num[i]);}scanf("%d", &ans);flag = 0;cnt2 = 0;DFS(1, num[0], ' ');if (flag) {printf("%d", num[0]);for (int i = 1; i < n; i++) {printf("%c%d", str[i – 1], num[i]);}printf("=%d", ans);}else printf("NO EXPRESSION");printf("\n");}return 0;}

每一件事都要用多方面的角度来看它

uva 10400 Game Show Math(深搜 )

相关文章:

你感兴趣的文章:

标签云: