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
来源
经典改编
上传者
张云聪
第一次由于用了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的更多相关文章

  1. Fedora 24中的日志管理

    Introduction Log files are files that contain messages about the system, including the kernel, servi ...

  2. CSharpGL(24)用ComputeShader实现一个简单的图像边缘检测功能

    CSharpGL(24)用ComputeShader实现一个简单的图像边缘检测功能 效果图 这是红宝书里的例子,在这个例子中,下述功能全部登场,因此这个例子可作为使用Compute Shader的典型 ...

  3. 【趣味分享】C#实现回味童年的24点算法游戏

    一.24点游戏玩法规则效果展示 1.初始化界面 2.开始游戏界面 3.游戏超时界面 4.查看答案界面 5.答对界面 6.答错界面 7.计算表达式的验证界面 8.一副牌算完开始新一副牌界面 到这里24点 ...

  4. C#开发微信门户及应用(24)-微信小店货架信息管理

    在前面微信小店系列篇<C#开发微信门户及应用(22)-微信小店的开发和使用>里面介绍了一些微信小店的基础知识,以及<C#开发微信门户及应用(23)-微信小店商品管理接口的封装和测试& ...

  5. [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下 ...

  6. mysql 5.6.24安装实例

    安装前准备工作: 1)编辑PATH路径 vim /etc/profile PATH=/home/mysql/bin:/home/mysql/lib:$PATH export PATH 2)生效PATH ...

  7. C语言-纸牌计算24点小游戏

    C语言实现纸牌计算24点小游戏 利用系统时间设定随机种子生成4个随机数,并对4个数字之间的运算次序以及运算符号进行枚举,从而计算判断是否能得出24,以达到程序目的.程序主要功能已完成,目前还有部分细节 ...

  8. .NET开发人员必看:提高ASP.NET Web应用性能的24种方法和技巧

    那性能问题到底该如何解决?以下是应用系统发布前,作为 .NET 开发人员需要检查的点. 1.debug=「false」 当创建 ASP.NET Web应用程序,默认设置为「true」.开发过程中,设置 ...

  9. [.net 面向对象程序设计进阶] (24) 团队开发利器(三)使用SVN多分支并行开发(下)

    [.net 面向对象程序设计进阶] (24) 团队开发利器(三)使用SVN多分支并行开发(下) 本篇导读: 接上篇继续介绍SVN的高级功能,即使用分支并行开发.随着需求的不断变更,新功能的增加.特别是 ...

  10. 1Z0-053 争议题目解析24

    1Z0-053 争议题目解析24 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 24.Which of the following information will be gath ...

随机推荐

  1. (转)dp动态规划分类详解

    dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间 ...

  2. SQL Server 2005数据库定期备份(非常详细)与 SQL Server 2005数据库备份定期清理

     SQL Server 2005数据库定期备份 分类: SQL Server 20052011-01-06 16:25 3320人阅读 评论(1) 收藏 举报 sql server数据库sqlserv ...

  3. codeforces 898F Hash

    F. Restoring the Expression time limit per test 2 seconds memory limit per test 256 megabytes input ...

  4. B3300 [USACO2011 Feb]Best Parenthesis 模拟

    这是我今天遇到最奇怪的问题,希望有人帮我解释一下... 一开始我能得90分: #include<iostream> #include<cstdio> #include<c ...

  5. PCB 工程系统 模拟windows域帐号登入

    一.需求描述: 对于PCB制造企业来说,基本都采用建立共享目享+域名管控权限,好像别的大多数行业都是这样的吧.呵呵 在实际应用中,经常会有这样的问题,自己登入的帐号没有共享目录的权限,但又想通过程序实 ...

  6. javascript 原型(prototype 、__proto__、函数、对象)

    一.类型 1.JavaScript中分为值类型(string/boolean/null/number/undefind).引用类型(数组.对象.函数): 2.数组.函数.对象都是对象: 对象是由函数创 ...

  7. CAS配置记录

    CAS配置(1)之证书配置 CAS配置(2)之主配置 CAS配置(3)之restful-api接入接口

  8. 小程序开发之搭建WebSocket的WSS环境(Apache+WorkerMan框架+PHP)

    最近公司的一个IoT项目用到了小程序的WSS协议环境,现在把整个的搭建开发过程分享给大家. 这里我们用的是WorkerMan框架,服务器是CentOS,Web服务器是Apache,开发语言是PHP. ...

  9. Yoga710笔记本Win10和Ubuntu系统共存

    联想yoga710默认安装了win10系统,且使用EFI分区格式,安装Ubuntu不是一般的困难,经公司小哥的帮助下,几次终于完成了Ubuntu和Win10 共存. 经过多次安装测试,暂时能运行成功的 ...

  10. 【sqli-labs】 less43 POST -Error based -String -Stacked with tiwst(POST型基于错误的堆叠变形字符型注入)

    和less42一样 login_user=&login_password=1');insert into users(id,username,password) value(15,'root' ...