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 ...
随机推荐
- Java -JVM:JVM百科
ylbtech-Java -JVM:JVM百科 JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机 ...
- 原生JS---1
js的历史 在上个世纪的1995年,当时的网景公司正凭借其Navigator浏览器成为Web时代开启时最著名的第一代互联网公司. 由于网景公司希望能在静态HTML页面上添加一些动态效果,于是叫Bren ...
- pdf 转成 一张图片
1.maven依赖 <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfb ...
- C - Arrival of the General
Problem description A Ministry for Defense sent a general to inspect the Super Secret Military Squad ...
- facade 模式和gateway模式的区别
原文:http://stackoverflow.com/questions/4422211/what-is-the-difference-between-facade-and-gateway-desi ...
- nginx编译安装新模块
nginx的模块是需要重新编译nginx,而不是像apache一样配置文件引用.so 这里以安装第三方ngx_http_google_filter_module模块为例 下载第三方扩展模块ngx_ht ...
- SQLServer 在存储过程里使用事务控制的简单小例子
alter proc sp_test( @name varchar(50))asbegin --开始事务 begin transaction --设置一个存储报错代码的变量 ...
- 浅谈Java三大框架与应用
前言:对于一个程序员来说,尤其是在java web端开发的程序员,三大框架:Struts+Hibernate+Spring是必须要掌握熟透的,因此,下面谈谈java三大框架的基本概念和原理. JAVA ...
- 【转载】java的常见类型转换
//Int型数字转换成String int num1=123456; //方法1 String str1=num1+""; System.out.println(str1); // ...
- python基础1 格式化输出
转载自:https://www.cnblogs.com/fat39/p/7159881.html %用法 1.整数输出 %o —— oct 八进制%d —— dec 十进制%x —— hex 十六进制 ...