24 Point game

时间限制:3000 ms  |  内存限制:65535 KB
难度:5
 
描述

There is a game which is called 24 Point game.

In this game , you will be given some numbers. Your task is to find an expression which have all the given numbers and the value of the expression should be 24 .The expression mustn't have any other operator except plus,minus,multiply,divide and the brackets.

e.g. If the numbers you are given is "3 3 8 8", you can give "8/(3-8/3)" as an answer. All the numbers should be used and the bracktes can be nested.

Your task in this problem is only to judge whether the given numbers can be used to find a expression whose value is the given number。

 
输入
The input has multicases and each case contains one line
The first line of the input is an non-negative integer C(C<=100),which indicates the number of the cases.
Each line has some integers,the first integer M(0<=M<=5) is the total number of the given numbers to consist the expression,the second integers N(0<=N<=100) is the number which the value of the expression should be.
Then,the followed M integer is the given numbers. All the given numbers is non-negative and less than 100
输出
For each test-cases,output "Yes" if there is an expression which fit all the demands,otherwise output "No" instead.
样例输入

2
4 24 3 3 8 8
3 24 8 3 3

样例输出

Yes
No

受不了,这么水的题搞了好久 - -,简直不能忍、
注意几个问题:
A: 括号怎么处理?由于可以乱排,我们搜索就相当于加了括号了,比如题目的 8/(3-8/3),我们搜索从8开始,8/3=2.6667,再3-2.6667=0.3333,再8/0.33333=24
B:注意加乘无方向,减和除有方向,所以有6个方向
C:注意最后判断结果的时候由于是浮点数,所以加一个精度,一般1e-8就可以了

见渣代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. using namespace std;
  6. #define EPS 1e-8
  7. #define N 10
  8.  
  9. int n;
  10. int flag;
  11. int vis[N];
  12. double s,a[];
  13.  
  14. void DFS(int num,double now)
  15. {
  16. if(flag) return;
  17. if(num==n+ && fabs(now-s)<=EPS)
  18. {
  19. flag=;
  20. return;
  21. }
  22. for(int i=;i<=n;i++)
  23. {
  24. if(!vis[i])
  25. {
  26. for(int j=;j<=;j++)
  27. {
  28. vis[i]=;
  29. if(j==) DFS(num+,now+a[i]);
  30. if(j==) DFS(num+,now-a[i]);
  31. if(j==) DFS(num+,a[i]-now);
  32. if(j==) DFS(num+,now*a[i]);
  33. if(j== && a[i]) DFS(num+,now*1.0/a[i]);
  34. if(j== && now) DFS(num+,a[i]*1.0/now);
  35. vis[i]=;
  36. }
  37. }
  38. }
  39. }
  40.  
  41. int main()
  42. {
  43. int T,i;
  44. scanf("%d",&T);
  45. while(T--)
  46. {
  47. flag=;
  48. scanf("%d%lf",&n,&s);
  49. for(i=;i<=n;i++)
  50. {
  51. scanf("%lf",&a[i]);
  52. }
  53. for(i=;i<=n;i++)
  54. {
  55. memset(vis,,sizeof(vis));
  56. vis[i]=;
  57. DFS(,a[i]);
  58. if(flag) break;
  59. }
  60. if(flag)
  61. cout<<"Yes\n";
  62. else
  63. cout<<"No\n";
  64. }
  65. return ;
  66. }

[NYOJ 43] 24 Point game的更多相关文章

  1. Nyoj 43 24 Point game 【DFS】

    24 Point game 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描写叙述 There is a game which is called 24 Point game ...

  2. nyoj 43 24 Point game(dfs暴力)

    描述 There Point game. In .The expression mustn't have any other operator except plus,minus,multiply,d ...

  3. FF D8 FF FE 00 24 47 00转图片

    String[] img = "FF D8 FF FE 00 24 47 00 9D 0C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F0 0 ...

  4. java处理中国气象数据,提取汇总陕西地区24小时各观测点的数据(csv格式)

    1.先贴一下气象数据的csv源格式,由于数据内容较多,就放一部分(china_sites_20150102.csv) date,hour,type,1001A,1002A,1003A,1004A,10 ...

  5. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  6. HTTP Cache

    最近在学习HTTP协议,看的书籍是<HTTP权威指南>,这本书讲的很不错,细节都讲的很透彻,虽然书本比较厚,因为讲的通俗易懂,所以比较有意思并不觉得枯燥.下面是读书后做的读书笔记. [图片 ...

  7. matlab 曲线拟合

    曲线拟合(转载:http://blog.sina.com.cn/s/blog_8e1548b80101c9iu.html) 补:拟合多项式输出为str 1.poly2str([p],'x') 2. f ...

  8. R自动数据收集第一章概述——《List of World Heritage in Danger》

      导包     library(stringr) library(XML) library(maps) heritage_parsed <- htmlParse("http://en ...

  9. java报表工具FineReport常用函数的用法总结(文本和日期函数)

    文本函数 CHAR CHAR(number):根据指定数字返回对应的字符.CHAR函数可将计算机其他类型的数字代码转换为字符. Number:用于指定字符的数字,介于1Number:用于指定字符的数字 ...

随机推荐

  1. 学习C++ Primer 的个人理解(十)

    标准库没有给每个容器都定义成员函数来实现 查找,替换等操作.而是定义了一组泛型算法,他们可以用于不同类型的元素或多种容器类型. 迭代器令算法不依赖与容器 算法永远不会执行容器的操作 算法本身不会执行容 ...

  2. 我爱工程化 之 gulp 使用(二)

    上一篇  介绍了gulp的安装.环境等配置.基本使用,那么现在,我们快走进 速8,深入了解吧...... 一.各种安装.环境配置.插件安装(参考上一篇文章) 二.项目基本目录结构 三.编写 gulpf ...

  3. [C#]获取一年中是第几个星期

    关键代码: /// <summary> /// 获取日期是一年中第几个星期 /// </summary> /// <param name="date" ...

  4. JS 日期格式转换

    //Json 数据年月日 返回 直接传入参数 如/Date(1379433600000)/ function GetDate(date) { if (date == null) return null ...

  5. 1. Window环境下 - 开发环境的配置: (安装Android Studio 2.1)

    0. Java简介: 1990年Sun公司预料嵌入式系统将在未来家用电器领域大显生手, 于是成立了一个由James Gosling领导的"Green计划"(首席科学家Bill Jo ...

  6. PHP程序员的技术成长规划(送给迷茫的你)

    按照了解的很多PHP/LNMP程序员的发展轨迹,结合个人经验体会,抽象出很多程序员对未来的迷漫,特别对技术学习的盲目和慌乱,简单梳理了这个每个阶段PHP程序员的技术要求,来帮助很多PHP程序做对照设定 ...

  7. UI设计的奥义

    个人觉得一个好的UI应该具备如下特点 1.符合人类认知行为 2.契合人体生物学 3.平滑,流畅 4.适当的交互会让你的应用更加成功 5.动态的内容才是招蜂引蝶的资本

  8. Challenge Checkio(python)—初尝python练习网站

    最近在找点python语言练习的网站,发现这个网站不错 http://www.checkio.org/ 页面设计的也比较漂亮,比较适合学习python的语法知识.不过注册这个网站 开始就得解决一个py ...

  9. python实用函数

    dir([obj]) 显示对象属性, 无参数显示全局变量的名字 help([obj]) 显示对象的文档字符串 int(obj) 将一个对象转换为整数 len(obj) 返回对象的长度 range([[ ...

  10. C语言字符知识狭区

    C语言字符在用户接口软件编程上经常用到,但是有一些狭区会让编程出现一些小BUG,现在总结与此. 1.'\\' 代表的是字符\,而'\'是不能代表字符\的.通常\后面都要跟上数字或者其他字母来表示一个特 ...