dfs全排列  加  模拟计算

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#define maxn 10010
using namespace std; bool vis[10];
int value[10], result, n, now[10],top;
char expr[100];
int check(int l, int r)
{
if(l > r)
return 0;
int cnt = 0,p = -1,flag = 0;
for(int i = l; i <= r; i++)
if(expr[i] == '(')
++cnt;
else if(expr[i] == ')')
--cnt;
else if((expr[i] == '+' || expr[i] == '-') && !cnt)
flag = 1, p = i;
else if((expr[i] == '*') && !cnt && !flag)
p = i;
if( p == -1 )
{
if( l == r )
{
return now[top++];
}
return check(l+1, r-1);
}
switch(expr[p])
{
case '+':
return check(l,p-1) + check(p+1,r);
case '-':
return check(l,p-1) - check(p+1,r);
case '*':
return check(l,p-1) * check(p+1,r);
}
return 0;
}
int dfs(int cur)
{
if( cur == n )
{
top = 0;
int len = strlen(expr);
if(check(0, len-1) == result)
return 1;
// printf("%d\n",check(0,len-1));
}
for(int i = 0; i < n; i++)
{
if( !vis[i] )
{
vis[i] = true;
now[cur] = value[i];
if(dfs(cur+1))
return 1;
vis[i] = false;
}
}
return 0;
}
int main()
{
while( scanf("%d",&n) == 1 )
{
if(n == 0)
{
scanf("%d",&result);
if(result == 0)
break;
}
for(int i = 0; i < n; i++) scanf("%d",&value[i]);
scanf("%d",&result);
scanf("%s",expr);
int flag = 0;
for(int i = 0; i < n; i++)
{
memset(vis, false, sizeof(vis));
vis[i] = true;
now[0] = value[i];
if(dfs(1))
{
flag = 1;
break;
}
vis[i] = false;
}
if( flag ) puts("YES");
else puts("NO");
}
return 0;
}

使用stl中的全排列函数

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define maxn 10010
using namespace std; int value[10], result, n, top;
char expr[100];
int check(int l, int r)
{
if(l == r)
return value[top++];
int cnt = 0,p = -1,flag = 0;
for(int i = l; i <= r; i++)
if(expr[i] == '(') cnt++;
else if(expr[i] == ')') cnt--;
else if((expr[i] == '+' || expr[i] == '-') && !cnt) flag = 1, p = i;
else if((expr[i] == '*') && !cnt && !flag) p = i;
if( p == -1 )
return check(l+1, r-1);
switch(expr[p])
{
case '+':
return check(l,p-1) + check(p+1,r);
case '-':
return check(l,p-1) - check(p+1,r);
case '*':
return check(l,p-1) * check(p+1,r);
}
return 0;
} int main()
{
while( scanf("%d",&n) == 1 )
{
if(n == 0)
{
scanf("%d",&result);
if(result == 0)
break;
}
for(int i = 0; i < n; i++) scanf("%d",&value[i]);
scanf("%d",&result);
scanf("%s",expr);
int flag = 0;
int len = strlen(expr);
sort(value, value+n);
do
{
top = 0;
if(check(0, len-1) == result)
{
flag = 1;
break;
}
}while(next_permutation(value, value+n));
if( flag ) puts("YES");
else puts("NO");
}
return 0;
}

hdu 4192的更多相关文章

  1. hdu 4192 (表达式求值)

    <题目链接> <转载于 >>>  > 题目大意: 给你n个数,和一个最终的结果,再给你一个含有n个不同变量的式子,问你这个式子最终能否得到指定的答案. 解题分 ...

  2. UVALive 4192/HDU 2959 Close Enough Computations 数学

    Close Enough Computations Problem Description The nutritional food label has become ubiquitous. A sa ...

  3. hdu 4193 Non-negative Partial Sums 单调队列。

    Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  4. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  6. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  7. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  8. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  9. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

随机推荐

  1. 用Dalvik指令集写个java类

    Dalvik指令集 .class public LCalculate;#定义类名 .super Ljava/lang/Object;#定义父类 .method public static main([ ...

  2. asp.net运行原理(一)总体概要

     1.浏览器发送请求报文到服务器,服务器接收到请求之后,根据请求报文头(url地址)的后缀名解析. 2.以iis服务器为例.他分为两种模式,经典模式和集成模式.主要是经典模式会将请求报文通过aspne ...

  3. IOS TextField设置大全

    //初始化textfield并设置位置及大小  //设置边框样式,只有设置了才会显示边框样式     text.borderStyle = UITextBorderStyleRoundedRect; ...

  4. c# 远程回收IIS应用池

    利用下列代码可实现IIS应用池的远程回收 var serverManager = ServerManager.OpenRemote(ip); var appPools = serverManager. ...

  5. 设置 MyEclipse 默认打开文件方式

         在web开发时,我们在打开JSP页面时,MyEclipse默认使用 Vistual 的方式打开.这种方式打开JSP页面时,很慢.在实际的开发过程中我们不想使用这种默认的方式打开JSP.设置如 ...

  6. OCI-DML-更新数据库中不存在的字段

    用gtest来测试oracle中oci方式的SQL语句操作,在测试update数据库中不存在的异常案例的时候,日志没有报错,但是结束后跳出了数据库连接 gtest也没有给出正常的结果,本身update ...

  7. Linux 简单字符设备驱动程序 (自顶向下)

    第零章:扯扯淡 特此总结一下写的一个简单字符设备驱动程序的过程,我要强调一下“自顶向下”这个介绍方法,因为我觉得这样更容易让没有接触过设备驱动程序的童鞋更容易理解,“自顶向下”最初从<计算机网络 ...

  8. poj 1659 Frogs' Neighborhood Havel-Hakimi定理 可简单图定理

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098136.html 给定一个非负整数序列$D=\{d_1,d_2,...d_n\}$,若存 ...

  9. 快速解码base64和utf-8的ASCII编码和URL解码

    看论坛上总是有人发乱七八糟的文字,根本看不懂,用下面的方法解密一下. 只要有浏览器的开发者工具就行了. UTF-16解码 console.log("\u5475\u5475") U ...

  10. Object-C - 类的定义

    http://www.cnblogs.com/zhangweia/archive/2011/11/01/2231549.html 1. 文件分为.h:定义接口,及其属性,方法说明. .m :是实现类. ...