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

分析:能够进行的运算为加。 减, 被减, 乘, 除。 被除六种运算。我们仅仅须要每次都取出没实用的两个数(能够是运算之后的值),进行运算,枚举就好了。

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
const double E = 1e-6;
using namespace std; double s[50], res;
bool vis[50]; int dfs(int m, int top){ //每次取出两个数然后将结果放入数组中,就等于每次减掉一个没有使用的数
if(m == 1){
if(fabs(res - s[top-1]) < E){
//printf("%lf...res %lf..sll\n", s[top-1], res);
return 1; }
return 0;
}
for(int i = 0; i < top-1; ++ i){
if(!vis[i]){
vis[i] = 1;
for(int j = i+1; j < top; ++ j){
if(!vis[j]){
vis[j] = 1; s[top] = s[i]+s[j];
if(dfs(m-1, top+1)) return 1; s[top] = s[i]-s[j];
if(dfs(m-1, top+1)) return 1; s[top] = s[j]-s[i];
if(dfs(m-1, top+1)) return 1; s[top] = s[i]*s[j];
if(dfs(m-1, top+1)) return 1; if(s[i] != 0){
s[top] = s[j]/s[i];
if(dfs(m-1, top+1)) return 1;
} if(s[j] != 0){
s[top] = s[i]/s[j];
if(dfs(m-1, top+1)) return 1;
} vis[j] = 0;
}
}
vis[i] = 0;
}
}
return 0;
} int main(){
int t;
scanf("%d", &t);
while(t --){
int n;
scanf("%d", &n);
scanf("%lf", &res);
for(int i = 0; i < n; ++ i) scanf("%lf", &s[i]);
memset(vis, 0, sizeof(vis));
if(dfs(n, n)) printf("Yes\n");
else printf("No\n");
}
return 0;
}

Nyoj 43 24 Point game 【DFS】的更多相关文章

  1. nyoj 43 24 Point game(dfs暴力)

    描述 There Point game. In .The expression mustn't have any other operator except plus,minus,multiply,d ...

  2. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  3. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

  4. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. 【dfs】P1331 海战

    题目描述 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞机.此外,巡洋船只和舰队将被派去保护海岸线.不幸的是因为种种原因,国防海军部仅有很少的几 ...

  6. 【dfs】p1731 生日蛋糕

    1441:[例题2]生日蛋搞 [题目描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体.设从下往上数第i(1≤i≤M)层蛋糕是半径为Ri, 高 ...

  7. 【dfs】LETTERS

    1212:LETTERS [题目描述] 给出一个roe×colroe×col的大写字母矩阵,一开始的位置为左上角,你可以向上下左右四个方向移动,并且不能移向曾经经过的字母.问最多可以经过几个字母. [ ...

  8. 洛谷P1605 迷宫【dfs】

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  9. 【dfs】BZOJ1703-[Usaco2007 Mar]Ranking the Cows 奶牛排名

    [题目大意] 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序,约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他 ...

随机推荐

  1. Windows离线安装.NET3.X

    Windows离线安装.NET3.X 当我们在Windows上安装软件的时候,总是会提示需要安装.NET3.X.而大多数人们选择在线安装,这样会很慢,不少人想到了离线安装的方式.其是只要你的电脑是Wi ...

  2. ASP.NET页面周期

    上图为ASP.NET页面生命周期图. 以下详细讲解一下ASP.NET的页面生命周期.   请求页 请求页发生在页生命周期之前.用户请求时,ASP.NET将确定是否需要分析和编译页面,或者是否可以在不运 ...

  3. sourceTree添加git密钥步骤

    给多个远程服务器比如https://github.com/wangjian2014/wjtest/blob/master/wj.txt添加public密钥 本地服务器添加private密钥     S ...

  4. RecyclerView更通用——listView的onItemClick,onLongItemClick,addHeaderView,addFooterView

    一.点击事件 setOnItemClickListener,setOnItemLongClickListener RecyclerView中虽然没有提供上面这两个接口,但是给我们提供了另外一个接口:O ...

  5. 用CALayer实现聚光灯效果

    效果图: 代码部分: #import "ViewController.h" @interface ViewController () @property (nonatomic, s ...

  6. 武汉科技大学ACM :1001: 零起点学算法34——继续求多项式

    Problem Description 输入1个正整数n, 计算1+(1+2)+(1+2+3)+...+(1+2+3+...+n) Input 输入正整数n(多组数据) Output 输出1+(1+2 ...

  7. 有关mysql数据库的编码

    今天在通过表单给php提交数据,然后插入到数据库中.网页与php的编码格式均为utf-8,在插入到数据库中时也设置了$this->query("set names utf8;" ...

  8. 关于nginx架构探究(3)

    Nginx 模块综述 Nginx 所有的代码都是以模块的新式组织的,包括核心模块和功能模块.Nginx加载模块的时候不想Apache一样动态加载,它是直接被编译到二进制执行文件中,所以,如果想要加载新 ...

  9. unity中的[xxxxxx]特性(Attributes)

    [SerializeField] 在Inspector版面中显示非public属性,并且序列化:若写在public前面,等于没写. [Obsolete("调用提示信息")] [No ...

  10. file_get_content、fsockopen和curl之间的优缺点

    file_get_content 优点:在抓取单个文件上,效率很高,返回没有头信息的文件. 缺点:在抓取远程文件时,和fopen一样容易出错.在抓取多个跨域文件时,未对DNS进行缓存,所以效率上不不高 ...