您的位置:首页 > 其它

uva 10400 Game Show Math(深搜 )

2015-02-15 21:23 246 查看

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 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个数求出的result。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: