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就可以了

见渣代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define EPS 1e-8
#define N 10 int n;
int flag;
int vis[N];
double s,a[]; void DFS(int num,double now)
{
if(flag) return;
if(num==n+ && fabs(now-s)<=EPS)
{
flag=;
return;
}
for(int i=;i<=n;i++)
{
if(!vis[i])
{
for(int j=;j<=;j++)
{
vis[i]=;
if(j==) DFS(num+,now+a[i]);
if(j==) DFS(num+,now-a[i]);
if(j==) DFS(num+,a[i]-now);
if(j==) DFS(num+,now*a[i]);
if(j== && a[i]) DFS(num+,now*1.0/a[i]);
if(j== && now) DFS(num+,a[i]*1.0/now);
vis[i]=;
}
}
}
} int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
flag=;
scanf("%d%lf",&n,&s);
for(i=;i<=n;i++)
{
scanf("%lf",&a[i]);
}
for(i=;i<=n;i++)
{
memset(vis,,sizeof(vis));
vis[i]=;
DFS(,a[i]);
if(flag) break;
}
if(flag)
cout<<"Yes\n";
else
cout<<"No\n";
}
return ;
}

[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. springmvc学习(二)——使用RequestMapper请求映射

    本次内容是@RequestMapping,后面会有实例代码 Spring MVC 使用 @RequestMapping 注解为控制器指定可以处理哪些 URL 请求在控制器的类定义及方法定义处都可标注@ ...

  2. C++输入结束

    通过判断输入是否等于EOF,可以结束输入. EOF 是个宏,其意思是:End Of File,文件尾标志. 从数值上来看,就是整数-1. 在C语言的头文件中对其进行了宏定义: libio.h:     ...

  3. mysql show processlist 显示mysql查询进程

    1.进入mysql/bin目录下输入mysqladmin processlist; 2.启动mysql,输入show processlist; 如果有 SUPER 权限,则可以看到全部的线程,否则,只 ...

  4. 排序算法TWO:快速排序QuickSort

    import java.util.Random ; /** *快速排序思路:用到了分治法 * 一个数组A[0,n-1] 分解为三个部分,A[0,p - 1] , A[p] , A[p + 1, n-1 ...

  5. 省市选择(基于zepto.js)

    效果如下: <div class="clList overflow-h mt75"> <select class="pull-left cl-35 se ...

  6. mysql查询语句分析 explain用法

    explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 使用方法,在select语句前加上explain就可以了,如: explai ...

  7. PHP中进制之间的互相转换

    常见的进制: 二进制   binary   ----->  bin 八进制   octal     ----->  oct 十进制   decimal ----->  dec 十六进 ...

  8. sprintf函数php的详细使用方法

    PHP sprintf() 函数 先说下为什么要写这个函数的前言,这个是我在微信二次开发的一个token验证文档也就是示例文档看到的一个函数,当时非常不理解,于是查了百度,但是很多结果都很笼统,结果也 ...

  9. hdu 5626 Clarke and points 数学推理

    Clarke and points Problem Description   The Manhattan Distance between point A(XA,YA) and B(XB,YB) i ...

  10. 一步步学习NHibernate(3)——NHibernate增删改查

    请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们配置了以下NHibernate的运行环境, 并介绍了NHibernate的中两个非常中重要的接口"I ...