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;
}

最新文章

  1. .NET WEB项目的调试发布相关
  2. Paxos算法与Zookeeper分析
  3. RAID-4与模2和
  4. linux下文件的复制、移动与删除
  5. with check option(视图 )
  6. iOS 静态类库项目的建立与使用
  7. [Python]sqlite3二进制文件存储问题(BLOB)(You must not use 8-bit bytestrings unless you use a text_factory...)
  8. jasmine 初探(一)
  9. ASP.NET WebAPI使用Swagger生成测试文档
  10. Button的几种常用的xml背景,扁平化,下划线,边框包裹,以及按压效果
  11. OpenCV 金字塔图像缩放
  12. 怎么停止yum安装并kill进程
  13. &lt;c:forEach&gt;详解
  14. node微信公众号开发---域名绑定
  15. 线性、逻辑回归的java实现
  16. VSS迁移详细教程
  17. nginx www解析失败问题解决
  18. [转]看懂Oracle执行计划
  19. ASP.NET WEB SERVICE 创建、部署与使用
  20. 加法变乘法——第六届蓝桥杯C语言B组(省赛)第六题

热门文章

  1. Redis Install
  2. C++经典题目:有n个人围成一圈,顺序排号,然后数数进行淘汰的解法和一些思考
  3. sun.misc.BASE64Encoder是内部专用 API, 可能会在未来发行版中删除
  4. DataGrid( 数据表格) 组件[2]
  5. .net程序开发人员必看的变量的命名规则
  6. servlet的含义和作用
  7. shell获取文件行数
  8. 华为 oj 表示数字(代码有参考)理解算法设计
  9. asp.net 发送邮件代码 System.Net.Mail
  10. poj2187 Beauty Contest(旋转卡壳)