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. 从vs中删除自带的Microsoft Git Provider

    https://researchaholic.com/2015/02/02/remove-the-microsoft-gitprovider-from-visual-studio-2013/ vs自带 ...

  2. WebSocket在Asp.Net中的例子

    环境 以下代码环境要求:win8或win10, .net4.5+IIS8 部署到IIS8上面 转到 Windows程序和功能 -打开Windows功能里面 IIS选项启动4.5 和WebSocket支 ...

  3. C++ this指针 全部

    在每一个成员函数中都包含一个特殊的指针,这个指针的名字是固定的.叫做this.它是指向本类对象的指针,它的值是当前被调用的成员函数所在的对象的起      始地址.例如:当调用成员函数a.volume ...

  4. windows php文件下载地址

    http://windows.php.net/downloads/releases/archives/

  5. input点击修改样式

    <input id="geren" type="button" value="个人奖励" style="BORDER-TOP ...

  6. ACM_城市交通线(简单并查集)

    城市交通线 Time Limit: 2000/1000ms (Java/Others) Problem Description: A国有n座城市,编号为1~n,这n个城市之间没有任何交通线路,所以不同 ...

  7. Spring Boot (19) servlet、filter、listener

    servlet.filter.listener,在spring boot中配置方式有两种:一种是以servlet3开始提供的注解方式,另一种是spring的注入方式. servlet注解方式 serv ...

  8. response.getWriter().write()乱码问题

    前台代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> & ...

  9. Centos6.6 安装Memcached

    一.环境介绍 1)Centos6.4 2) memcached-1.4.24 二.部署安装 计划具体部署步骤: 步骤1:安装 步骤2:配置 步骤3:运行 步骤4:检查 现在开始: 1)安装 $ yum ...

  10. C#自动缩进排列代码的快捷键 c# 代码重新排版 变整齐

    C#自动缩进排列代码的快捷键:  ctrl + k + d 1.小技巧, 可以把最后一个}去掉, 重新写下,就可以达到排版的效果. 2.快捷键:编辑-高级-设置文档的格式 快捷键Ctrl+E,D,设置 ...