题目大意:给出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 ;
}

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

UVa 10400 - Game Show Math的更多相关文章

  1. UVA 10400 Game Show Math (dfs + 记忆化搜索)

    Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...

  2. UVa 10400 - Game Show Math 游戏中的数学 dfs+判重

    题意:给出一些数字和一个目标数字,要求你在数字间添加+-*/,让表达式能达到目标数字,运算符号的优先级都是一样的. 由于数据量很大,本来想用map<string>判重的,结果还是超时了,然 ...

  3. UVa 10400 记忆化搜索

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...

  4. UVa 1595 Symmetry (set && math)

    题意:给出n个在直角坐标系上的点,问你能不能找出一条竖轴(即垂直于x的轴)使得所有的点根据这条轴对称,能则输出YES,否则输出NO 分析:首先需要找到对称轴的值,将所有n个点的x轴的值加起来然后去除以 ...

  5. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  6. UVa10023手动开大数平方算法

    题目链接:UVa 10023 import java.math.BigInteger; import java.util.Scanner; public class Main { public sta ...

  7. UVA 1397 - The Teacher&#39;s Side of Math(高斯消元)

    UVA 1397 - The Teacher's Side of Math 题目链接 题意:给定一个x=a1/m+b1/n.求原方程组 思路:因为m*n最多20,全部最高项仅仅有20.然后能够把每一个 ...

  8. Math - Uva 11300 Spreading the Wealth

    Spreading the Wealth Problem's Link ---------------------------------------------------------------- ...

  9. UVa 10773 - Back to Intermediate Math

    题目:渡河问题.给你河水宽度,水流速度,求垂直渡河与最快渡河的时间差. 分析:物理题,数学题. 最快渡河情况,传垂直运动,垂直渡河,船的水平分速度和水流速度抵消. 说明:注意水流速度不能为0. #in ...

随机推荐

  1. 转:Eclipse Debug 界面应用详解——Eclipse Debug不为人知的秘密

    今天浏览csdn,发现一文详细的描述了Eclipse Debug中的各个知识点,非常详尽!特此记录. Eclipse Debug不为人知的秘密 http://blog.csdn.net/mgoann/ ...

  2. 服务器修改 ssh 22端口

    我们首先修改配置文件,让ssh 通知支持2个端口访问 ,22 和 23456 端口. Port 22 Port 23456 为什么要这样: 万一我用 23456端口替换了22端口,但是 23456正好 ...

  3. PKI 笔记

    PKI – Public Key Infrastructure , 通常翻译为公钥基础设施. PKI 安全平台提供的4个服务,来保证安全的数据,分别是: l  身份识别 l  数据保密 l  数据完整 ...

  4. make[1]: *** [/workopenwrt/trunk/staging_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/stamp/.tools_install_nnnnn] Error 2 make[1]: Leaving directory `/work/openwrt/trunk' make: *** [world]

    主要原因是编译时未连上网,编译时需要下载些插件,连接网后,重启下系统再编译下.

  5. PAT (Advanced Level) 1072. Gas Station (30)

    枚举一下选的位置,每次算一下就可以了. #include<cstdio> #include<cstring> #include<cmath> #include< ...

  6. Python之路:线程池

    版本一 #!/usr/bin/env  python # --*--coding:utf-8 --*-- import Queue import threading class ThreadPool( ...

  7. SystemUI简介

    http://wenku.baidu.com/link?url=p9hBaL4tmc6Z6fAxar23459qPEv3EqDBCW71SmdrphTA0vU02ZWVayNkEItvkP1WSk4L ...

  8. read cache return null

    http://stackoverflow.com/questions/8063590/unexpected-behaviour-after-memcached-server-restarts-how- ...

  9. word 书签排序算法

    直接上代码 /// <summary> /// 通过计算插入引文的位置格式化合适的引文序号 /// </summary> /// <returns></ret ...

  10. CSS Font-family常用设置

    font-family: "Avenir Next", Avenir, "Helvetica Neue", Helvetica, "Lantinghe ...