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 - 来源
- 经典改编
- 上传者
- 张云聪
- 第一次由于用了vector数组覆盖的方法,太蠢了,大量的空间移动和数据拷贝,TLE
- 超时代码:
-
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 103
#define LLL 1000000000
#define INF 1000000009
#define eps 0.00000001
/*
给一些数字,能否用利用+-*和/ 将这些数字组成一个特定的值,可以利用括号改变求值顺序
注意到给的数字个数很小 最多5个
每次将其中两个抽出来运算,然后将结果插入进去
*/
double aim;
int n, T;
vector<double> v,tmp;
bool f;
void print(vector<double> &a)
{
for (int i = ; i < a.size(); i++)
{
if (i) printf(" ");
printf("%lf", a[i]);
}
cout << endl;
}
void dfs(int x)
{
if (x == )
{
//print(tmp);
double che = tmp[] - aim;
if (tmp[] - aim > -eps&&tmp[] - aim < eps)
{
f = true;
//cout << "sdaasddddddddddddddddddddddddddddddddddddddddddddddddddddd" << endl;
}
return;
}
//print(tmp);
vector<double> h = tmp;
double a, b, mul, add, sub1, sub2, div1, div2;
for (int i = ; i < x; i++)
{
for (int j = i + ; j < x; j++)
{
tmp = h;
a = tmp[i], b = tmp[j];
mul = a*b, add = a + b;
sub1 = a - b, sub2 = b - a;
if (a != 0.0)
div1 = b / a;
else
div1 = INF;
if (b != 0.0)
div2 = a / b;
else
div2 = INF;
tmp.erase(tmp.begin() + i);
tmp.erase(tmp.begin() + j - );
vector<double> r = tmp;
for (int i = ; i <= tmp.size(); i++)
{
tmp.insert(tmp.begin() + i, add);
dfs(tmp.size());
tmp = r;
if (f) return;
tmp.insert(tmp.begin() + i, mul);
dfs(tmp.size());
tmp = r;
if (f) return;
tmp.insert(tmp.begin() + i, sub1);
dfs(tmp.size());
tmp = r;
if (f) return;
if (sub2-sub2>-eps&&sub1-sub2<eps)
{
tmp.insert(tmp.begin() + i, add);
dfs(tmp.size());
tmp = r;
}
if (f) return;
tmp.insert(tmp.begin() + i, div1);
dfs(tmp.size());
tmp = r;
if (f) return;
if (div1-div2<eps&&div1-div2>-eps)
{
tmp.insert(tmp.begin() + i, div2);
dfs(tmp.size());
tmp = r;
}
if (f) return;
}
}
}
}
int main()
{
scanf("%d", &T);
while (T--)
{
v.clear();
scanf("%d%lf", &n, &aim);
int t;
for (int i = ; i < n; i++)
{
scanf("%d", &t);
v.push_back(t);
}
f = false;
tmp = v;
dfs(n);
if (f)
printf("Yes\n");
else
printf("No\n");
}
return ;
}
24 Point game的更多相关文章
- Fedora 24中的日志管理
Introduction Log files are files that contain messages about the system, including the kernel, servi ...
- CSharpGL(24)用ComputeShader实现一个简单的图像边缘检测功能
CSharpGL(24)用ComputeShader实现一个简单的图像边缘检测功能 效果图 这是红宝书里的例子,在这个例子中,下述功能全部登场,因此这个例子可作为使用Compute Shader的典型 ...
- 【趣味分享】C#实现回味童年的24点算法游戏
一.24点游戏玩法规则效果展示 1.初始化界面 2.开始游戏界面 3.游戏超时界面 4.查看答案界面 5.答对界面 6.答错界面 7.计算表达式的验证界面 8.一副牌算完开始新一副牌界面 到这里24点 ...
- C#开发微信门户及应用(24)-微信小店货架信息管理
在前面微信小店系列篇<C#开发微信门户及应用(22)-微信小店的开发和使用>里面介绍了一些微信小店的基础知识,以及<C#开发微信门户及应用(23)-微信小店商品管理接口的封装和测试& ...
- [MySQL Reference Manual] 24 MySQL sys框架
24 MySQL sys框架 24 MySQL sys框架 24.1 sys框架的前提条件 24.2 使用sys框架 24.3 sys框架进度报告 24.4 sys框架的对象 24.4.1所有sys下 ...
- mysql 5.6.24安装实例
安装前准备工作: 1)编辑PATH路径 vim /etc/profile PATH=/home/mysql/bin:/home/mysql/lib:$PATH export PATH 2)生效PATH ...
- C语言-纸牌计算24点小游戏
C语言实现纸牌计算24点小游戏 利用系统时间设定随机种子生成4个随机数,并对4个数字之间的运算次序以及运算符号进行枚举,从而计算判断是否能得出24,以达到程序目的.程序主要功能已完成,目前还有部分细节 ...
- .NET开发人员必看:提高ASP.NET Web应用性能的24种方法和技巧
那性能问题到底该如何解决?以下是应用系统发布前,作为 .NET 开发人员需要检查的点. 1.debug=「false」 当创建 ASP.NET Web应用程序,默认设置为「true」.开发过程中,设置 ...
- [.net 面向对象程序设计进阶] (24) 团队开发利器(三)使用SVN多分支并行开发(下)
[.net 面向对象程序设计进阶] (24) 团队开发利器(三)使用SVN多分支并行开发(下) 本篇导读: 接上篇继续介绍SVN的高级功能,即使用分支并行开发.随着需求的不断变更,新功能的增加.特别是 ...
- 1Z0-053 争议题目解析24
1Z0-053 争议题目解析24 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 24.Which of the following information will be gath ...
随机推荐
- B2002 [Hnoi2010]Bounce 弹飞绵羊 分块
原来做过,看大家都做这道题都热情高涨,沈爷爷debug这道题4天,作为告诉他这个题的人,我还有点不好意思...我自己也就做了一个小时. 其实这个题思路还好,就是维护每个点的出块次数和跳出块的位置,然后 ...
- easyui+struts2:datagrid无法不能得到数据
转自:https://bbs.csdn.net/topics/390980437 easyui+struts2:datagrid无法访问到指定action: userlist.jsp部分代码: XML ...
- bzoj3297[USACO2011 Open]forgot(dp + string)
3297: [USACO2011 Open]forgot Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 137 Solved: 94[Submit] ...
- B - Chat room
Problem description Vasya has recently learned to type and log on to the Internet. He immediately en ...
- Java中last_insert_id的使用
last_insert_id的作用是:在当前表中的主键是自增时,插入一条新记录的同时使用last_insert_id可以获取当前的新记录的主键id. 下面是一个例子: import java.sql. ...
- 配置 Sybase数据源
1.Start-- > All Programs -- > Sybase - -> Connectivity --> click ‘Open Client Directory ...
- CSS框架Bootstrap
作为一个软件开发人员,经常接触和使用框架是再平常的事情不过了.但是这些框架基本都是和语言相关的,比如WEB框架SpringMVC,JavaEE框架Spring,ORM框架Hibernate,还有Jav ...
- 终极解决VS2015 安装失败问题,如 安装包损坏或丢失
1.去微软官网下载完成ISO镜像,最好不要在线安装, 打开官方链接 https://www.visualstudio.com/zh-cn/downloads/download-visual-studi ...
- GrepWin:Win7下的文本替换工具
工作环境退回到Win7之后,内容查找功能非常不给力,推荐一个文本内容查找工具grepWin. Win7下的文本查找/替换工具: grepWin
- semiautomatic annotated tools
在进行实验图像取样时,可能会用到大量的标签样本,拍摄大量图片进行手工标注要消耗大量时间,半自动化的标注工具可以节省一些时间. 原文链接:http://blog.sina.com.cn/s/blog_6 ...