UVa 10400 - Game Show Math

  题目大意:给出n(n<100)个正整数和一个目标数,按照给出数的顺序,运用+、-、*、/四则运算(不考虑优先级),判断能否得出所要的结果。

  首先考虑的就是暴力枚举,不过时间复杂度为O(4n),会超时。由于题目限定中间运算结果在[-32000, 32000]内,当n到一定程度后(4n远远大于64000后),就会存在大量的重复子问题,可以通过记忆化搜索的方法去重,具体到这个问题就是vis数组的运用。

 #include <cstdio>
#include <cstring>
#define MAXN 100+10 long long a[MAXN], target;
char op[MAXN];
int n;
bool ok, vis[MAXN][*+]; bool judge(int cur, int n)
{
return n >= - && n <= && !vis[cur][n+];
} void dfs(int cur, long long res)
{
if (cur == n )
{
if (res == target) ok = true;
return;
}
if (!ok && judge(cur, res+a[cur]))
{
op[cur] = '+';
vis[cur][res+a[cur]+] = true;
dfs(cur+, res+a[cur]);
}
if (!ok && judge(cur, res-a[cur]))
{
op[cur] = '-';
vis[cur][res-a[cur]+] = true;
dfs(cur+, res-a[cur]);
}
if (!ok && judge(cur, res*a[cur]))
{
op[cur] = '*';
vis[cur][res*a[cur]+] = true;
dfs(cur+, res*a[cur]);
}
if (!ok && res % a[cur] == && judge(cur, res/a[cur]))
{
op[cur] = '/';
vis[cur][res/a[cur]+] = true;
dfs(cur+, res/a[cur]);
}
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%lld", &a[i]);
scanf("%lld", &target);
ok = false;
memset(vis, , sizeof(vis));
vis[][a[]+] = true;
dfs(, a[]);
if (ok)
{
for (int i = ; i < n-; i++)
printf("%lld%c", a[i], op[i+]);
printf("%lld=%lld\n", a[n-], target);
}
else printf("NO EXPRESSION\n");
}
return ;
}

  也可以用递推的方法求解,不过在打印结果的时候要麻烦一些,二者的思想是一样的。

上一篇:Jquery 实现Xml文件内容处理


下一篇:Python单元测试框架之pytest -- 断言