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. 16_会话技术_Session案例

    [购物车中的信息保存] [Book.java] package com.Higgin.shopping; public class Book { private String id; private ...

  2. VC++代码的汇编分析(一)

    VC++代码是最接近汇编指令的高级语言,为了更加准确和深刻理解VC++编码中所涉及的很多技术概念和编译器参数的含义,从汇编指令层面进行剖析和解读,有助于开发者更加准确.直观.深刻理解高级语言中很多概念 ...

  3. RX学习笔记:在FreeCodeCamp的学习

    FreeCodeCamp https://www.freecodecamp.com 2016-07-03 前几日在Github浏览时,偶然看到一个叫FreeCodeCamp的开源项目,进去该网站之后感 ...

  4. H5 APP开发必读,20个你不知道的Html5新特征和窍门

    Jeffrey Way曾发表过一篇博文<28 HTML5 Features, Tips, and Techniques you Must Know >讲述了28个HTML5特征.窍门和技术 ...

  5. 使用python抓取有路网图书信息(原创)

    以前挺喜欢去有路网买二手书的,但是有路网有个缺陷,就是放在图书列表中的书很多都没货了,尤其是一些热门的方向,比如android,在列表中的书大多都没有货了,你必须一个一个点进入查看详细信息才能得知图书 ...

  6. MySQL基础学习之数据库

    创建一个新的数据库 create database 数据库名称; 查看所有数据库 show databases; 删除数据库 drop database 数据库名称

  7. 转:testlink 环境搭建(傻瓜版)

    testlink 环境搭建(傻瓜版) 2011-11-24 22:23 by 虫师, 12322 阅读, 4 评论, 收藏, 编辑 今天抽了点时间把testlink 环境搭建了一下,一直觉得这东西不怎 ...

  8. Java 内存区域

    Java虚拟机锁管理的内存包括以下几个运行时数据区域: 1.程序计数器: 程序计数器是一块较小的内存空间,它的作用可以看做是当前线程所执行的字节码的行号指示器.在虚拟机的概念模型里,字节码解释器工作时 ...

  9. windows下常用快捷键

    windows下常用快捷键: 显示桌面: win徽标+D 打开资源管理器: win徽标+E 打开任务管理器: Ctrl+Alt+delete 任务切换: Alt+Tab 应用程序中窗口之间的切换: C ...

  10. pywinauto二次封装(pywinnat.py)

    将pywinauto常用方法进行封装,使得pywinauto用起来更简单 #头文件的引入 from pywinauto import application from pywinauto import ...