nyoj43 24 Point game(DFS)
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 - 来源
- 经典改编
- 上传者
- 张云聪
感觉很经典的一道深搜题。
昨天下午自己起初给sum定义了一个初始值0。后来发现第二个測试用例不正确。
。
。那时候实在是不知道哪里错了
以至于昨天晚上做梦都梦到自己AC了这道题 哈哈

我自己认为自己的方法没有错。于是今天早上就百度搜搜,发现没人和我的思想一样
....别人都说是编程之美这本书上的。
。但是我也没看过
他们的思想都是在一个长度为n的数组中找到随意两个数。然后求和,然后把和再插入数组中。同一时候n--。
直到n=1.
o(︶︿︶)o ,我就仅仅好关闭了百度。
看着自己的程序慢慢调试,而且输出每个可能的结果值。
。后来还真被我发现了。。
由于我初始的sum为0的缘故,
假设仅仅有一个元素5,本来该仅仅有一个结果的。。但是因为我的原因会出现0-5=-5,0+5=5,0*5=0。0/5=0。
。。。傻了傻了。。
于是就又改了下AC了 。。嘿嘿。看来我的梦还是挺灵验的。。
看代码吧:
#include <stdio.h>
#include <math.h>
#include <string.h>
int flag,n;
double a[10],aim;//double型的把 由于相除嘛 难免会造成精度损失
int vis[10];
void dfs(double sum)
{
int cnt=0;
for(int i=0;i<n;i++)
if(vis[i])
cnt++;
if(cnt==n&&fabs(sum-aim)<0.0000001)//一个推断的条件。 当全部元素都用上。 并且sum和aim相差非常小
{
flag=1;
return ;
}
for(int i=0;i<n;i++)
{
if(!vis[i]&&!flag)
{
vis[i]=1;
dfs(sum+a[i]);//
dfs(sum-a[i]);//
dfs(sum*a[i]);//
dfs(sum/a[i]);//
dfs(a[i]/sum);//
dfs(a[i]-sum);//对于这个数sum仅仅能有6种情况
vis[i]=0;
}
}
}
int main()
{
int ncase;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d %lf",&n,&aim);
for(int i=0;i<n;i++)
scanf("%lf",&a[i]);
flag=0;
for(int i=0;i<n;i++)//
{
memset(vis,0,sizeof(vis));
vis[i]=1;
if(!flag)
dfs(a[i]);//起初的sum不应该为0,应该是数组中的某一个数、、
}//起初我没有上面这个for循环,仅仅有一个dfs(0);华丽丽的wa...
if(flag)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
update:在杭电上做了一道题 今天细致想了想 这道题我的做法始终还是有些欠缺。即使能在nyoj上A了。
所以读者自行取舍。我还是忽略了 比方1 13 5 9 我的程序是执行不出来的。由于我的程序在运算的时候一定要和当前的结果有关。
所以假设想学习新知识看我在杭电上的题解把点击打开链接这两道题一样。也当巩固一下知识。
nyoj43 24 Point game(DFS)的更多相关文章
- cdoj 1252 24点游戏 dfs
24点游戏 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1252 Descr ...
- 24点游戏&&速算24点(dfs)
24点游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...
- DFS Used%: NaN%问题
一.问题描述: [root@master sbin]# hdfs dfsadmin -report Configured Capacity: 0 (0 B) Present Capacity: 0 ( ...
- LeetCode Sum of Left Leaves
原题链接在这里:https://leetcode.com/problems/sum-of-left-leaves/ 题目: Find the sum of all left leaves in a g ...
- Hadoop:搭建hadoop集群
操作系统环境准备: 准备几台服务器(我这里是三台虚拟机): linux ubuntu 14.04 server x64(下载地址:http://releases.ubuntu.com/14.04.2/ ...
- Hadoop2.6.0完全分布式安装
本文地址:http://www.cnblogs.com/myresearch/p/hadoop-full-distributed-operation.html,转载请注明源地址. 我这边是使用了两台主 ...
- Ubuntu_10.04下Hadoop-0.20.2集群配置手册
Ubuntu_10.04下Hadoop-0.20.2集群配置手册 一.软硬件环境的准备 下面的文章来自hadoopor.com,我先交待一下我自己的环境: 两台机器,每台机器上面两个虚机(vmware ...
- hadoop默认3个核心配置文件说明
1 获取默认配置 配置hadoop,主要是配置core-site.xml,hdfs-site.xml,mapred-site.xml三个配置文件,默认下来,这些配置文件都是空的,所以很难知 ...
- Hadoop2.3.0具体安装过程
前言: Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS.HDFS有高容错性的特点,并且设计用来部署在低廉的(low-co ...
随机推荐
- [整理]菜鸟教程:docker使用笔记
- docker # 查看 docker stats 指令的具体使用方法 - docker stats --help # 运行一个web应用 - docker pull training/webapp ...
- Jeddict:从服务器的验证过程,思考学习新事物的套路
结合一路研究Jeddict使用的过程经验来看,在这里说一下关于服务器配置的东西.在我们团队,最开始用这个插件的时候,因为公司用的应用服务器是Jboss EAP 7,所以,我们自然而然的,想当然的直接使 ...
- NOJ——1659求值(log10取对数+floor取整数部分+可有可无的快速幂)
[1659] 求值 时间限制: 1000 ms 内存限制: 65535 K 问题描述 给你三个数a,b,c,求a的b次的前c位数(不够c位输出全部即可) 输入 输入数据有多组,每组占一行,有三个整数, ...
- BZOJ4566 [Haoi2016]找相同字符 【后缀数组】
题目 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同. 输入格式 两行,两个字符串s1,s2,长度分别为n1,n2.1 & ...
- 自己关于Django的一些实践
一 render() redirect() HttpResponse() 响应 是个什么东西 def login(request): if request.method=='POST': userna ...
- Sum BZOJ 3944
Sum [问题描述] 给定一个正整数 N ( N <= 231 - 1 ) 求: [输入格式] 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询 ...
- 金鹰教程网 FLASH8.0(AS)视频教程(下载地址)自认为最好的一个Flash教程
原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] 可以用迅雷新建批量任务下载,很方便的。 金鹰教程网 FLASH8.0教学视频 到目前(2008年7月29日21:2 ...
- Promise简单实现--摘抄
Promise 看了些promise的介绍,还是感觉不够深入,这个在解决异步问题上是一个很好的解决方案,所以详细看一下,顺便按照自己的思路实现一个简单的Promise. Promise/A+规范: 首 ...
- AC日记——花店橱窗布置 codevs 1028
题目描述 Description 假设以最美观的方式布置花店的橱窗,有F束花,V个花瓶,我们用美学值(一个整数)表示每束花放入每个花瓶所产生的美学效果.为了取得最佳的美学效果,必须使花的摆放取得最大的 ...
- AC日记——圆桌聚餐 cogs 729
729. [网络流24题] 圆桌聚餐 ★★ 输入文件:roundtable.in 输出文件:roundtable.out 评测插件时间限制:1 s 内存限制:128 MB «问题描述: ...