[NYOJ 43] 24 Point game
24 Point game
- 描述
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的更多相关文章
- Nyoj 43 24 Point game 【DFS】
24 Point game 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描写叙述 There is a game which is called 24 Point game ...
- nyoj 43 24 Point game(dfs暴力)
描述 There Point game. In .The expression mustn't have any other operator except plus,minus,multiply,d ...
- 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 ...
- java处理中国气象数据,提取汇总陕西地区24小时各观测点的数据(csv格式)
1.先贴一下气象数据的csv源格式,由于数据内容较多,就放一部分(china_sites_20150102.csv) date,hour,type,1001A,1002A,1003A,1004A,10 ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- HTTP Cache
最近在学习HTTP协议,看的书籍是<HTTP权威指南>,这本书讲的很不错,细节都讲的很透彻,虽然书本比较厚,因为讲的通俗易懂,所以比较有意思并不觉得枯燥.下面是读书后做的读书笔记. [图片 ...
- matlab 曲线拟合
曲线拟合(转载:http://blog.sina.com.cn/s/blog_8e1548b80101c9iu.html) 补:拟合多项式输出为str 1.poly2str([p],'x') 2. f ...
- R自动数据收集第一章概述——《List of World Heritage in Danger》
导包 library(stringr) library(XML) library(maps) heritage_parsed <- htmlParse("http://en ...
- java报表工具FineReport常用函数的用法总结(文本和日期函数)
文本函数 CHAR CHAR(number):根据指定数字返回对应的字符.CHAR函数可将计算机其他类型的数字代码转换为字符. Number:用于指定字符的数字,介于1Number:用于指定字符的数字 ...
随机推荐
- 2016ACM竞赛训练暑期课期末考试 a题
描述 给出n个正整数,任取两个数分别作为分子和分母组成最简真分数,编程求共有几个这样的组合. 输入 第一行是一个正整数n(n<=600).第二行是n个不同的整数,相邻两个整数之间用单个空格隔开. ...
- 学习C++ Primer 的个人理解(三)
第三章,主要内容是字符串和数组.感觉作者的意图是希望读者可以早一点可以写出简单的小程序,并且可以早点接触迭代器这种思想. 在我看来,这种内容的难度并不大. 对于编程来说,最重要的应该是思想,类似vec ...
- 基于libuv库的UDP收/发广播消息代码实现
uv_send(发送端): #include "uv.h" #include "task.h" #include <stdio.h> #includ ...
- Android开发系列之ListView
上篇博客解决了Android客户端通过WebService与服务器端程序进行交互的问题,这篇博客重点关注两个问题,一个是Android应用程序如何与本机文件型数据库SQLite进行交互,另一问题则是如 ...
- HTML5元素拖拽实现示例
HTML5现在前端圈中,已然成为一个不那么新的技术词汇了,很多公司也把HTML5也当成了硬性的技能要求,但是很多前端恐怕都不了解HTML5的拖拽怎么实现吧. 看了下极客学院的视频,大概的了解了下思路. ...
- /dev/null 2>&1 解释(转)
cmd >a 2>a 和 cmd >a 2>&1 为什么不同? cmd >a 2>a :stdout和stderr都直接送往文件 a ,a文件会被打开两遍, ...
- PHP字符
匹配查找 strstr strpos 通常用在表单验证里面可以用到 substr 正值表达式匹配 preg_mathc(), preg)mathc_all() , preg_grep() 编码格式的转 ...
- find grep
grep grep -rn "hello,world!" * #递归查找当前目录下所有包含hello,world的文件 grep -C number pattern files : ...
- [Python][flask][flask-wtf]关于flask-wtf中API使用实例教程
简介:简单的集成flask,WTForms,包括跨站请求伪造(CSRF),文件上传和验证码. 一.安装(Install) 此文仍然是Windows操作系统下的教程,但是和linux操作系统下的运行环境 ...
- 使用grub硬盘重装ubuntu
电脑一直是双系统,Windows8 + Ubuntu13.04,昨天瞎搞,更新了一下QT,结果就不能开机了...真心难. 老系统坏了也好,正好可以装个Ubuntu14.04LTS.手边没有U盘,可以把 ...