http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=43

                  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 <iostream>
#include <cstring>
#include <cmath>
using namespace std;
double a[10];
int visit[10];
int n, ans; int dfs(double sum, int ct){ //注意sum是中间值,可能为小数
if(ct == n){
if(fabs(sum - ans) < 1e-6){ //fabs()判断小数更准确
return 1;
}
else{
return 0;
}
}
for(int i = 0; i < n; i++){
if(visit[i] == 0){
visit[i] = 1;
int flag = 0;
flag = dfs(sum + a[i], ct + 1); if(flag) return 1; //每个dfs应该接收返回值,避免后续计算,同时要传递返回到mian里面
flag = dfs(sum - a[i], ct + 1); if(flag) return 1;
flag = dfs(a[i] - sum, ct + 1); if(flag) return 1; //减法,除法要考虑顺序,出发要考虑除零的情况
flag = dfs(sum * a[i], ct + 1); if(flag) return 1;
if(a[i]){
flag = dfs(sum / a[i], ct + 1); if(flag) return 1;
}
if(sum){
flag = dfs(a[i] / sum, ct + 1); if(flag) return 1;
}
visit[i] = 0;
}
}
return 0;
} int main(){
int t;
cin >> t;
while(t--){
cin >> n >> ans;
for(int i = 0; i < n; i++){
cin >> a[i];
}
int flag = 0;
for(int i = 0; i < n; i++){
memset(visit, 0, sizeof(visit)); //必须用这个,因为dfs有执行过程用了return,导致找到结果后直接退出,并未将visit还原
visit[i] = 1;
if(dfs(a[i], 1)){
flag = 1;
break;
}
visit[i] = 0;
}
if(flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}

  

34- 24 Point game的更多相关文章

  1. /usr/include/glib-2.0/glib/gtypes.h:34:24: fatal error: glibconfig.h: No such file or directory

    cc -DDEBUG -mtune=core2 -O2 \ -onvideo nvideo.c \ -I/usr/include/atk-1.0 \ -I/usr/include/cairo \ -I ...

  2. java处理中国气象数据,提取汇总陕西地区24小时各观测点的数据(csv格式)

    1.先贴一下气象数据的csv源格式,由于数据内容较多,就放一部分(china_sites_20150102.csv) date,hour,type,1001A,1002A,1003A,1004A,10 ...

  3. 求N个数的最大公约数和最小公倍数(转)

    除了分解质因数,还有另一种适用于求几个较小数的最大公约数.最小公倍数的方法 下面是数学证明及算法实现 令[a1,a2,..,an] 表示a1,a2,..,an的最小公倍数,(a1,a2,..,an)表 ...

  4. Linux 磁盘自检介绍

    在Linux系统中,有时候重启会耗费非常长的时间,如果你进一步检查细节,就会发现绝大部分时间都耗费在磁盘自检(fsck)上了,有时候遇到时间比较紧急的情况,磁盘自检耗费的时间非常长,真的是让人心焦火急 ...

  5. 软件工程(FZU2015)赛季得分榜,第11回合(beta冲刺+SE总结)

    目录 第一回合 第二回合 第三回合 第四回合 第五回合 第6回合 第7回合 第8回合 第9回合 第10回合 第11回合 增补作业 积分规则 积分制: 作业为10分制,练习为3分制:alpha30分:b ...

  6. aes加密C语言

    /** * \file aes.h * * \brief AES block cipher * * Copyright (C) 2006-2010, Brainspark B.V. * * This ...

  7. ZAM 3D 制作简单的3D字幕 流程(二)

    原地址:http://www.cnblogs.com/yk250/p/5663907.html 文中表述仅为本人理解,若有偏差和错误请指正! 接着 ZAM 3D 制作简单的3D字幕 流程(一) .本篇 ...

  8. GCD、dispatch函数介绍

    iOS多线程的方法有3种: NSThread NSOperation GCD(Grand Central Dispatch) 其中,由苹果所倡导的为多核的并行运算提出的解决方案:GCD能够访问线程池, ...

  9. 【Android端 APP GPU过度绘制】GPU过度绘制及优化

    一.Android端的卡顿 Android端APP在具体使用的过程中容易出现卡顿的情况,比如查看页面时出现一顿一顿的感受,切换tab之后响应很慢,或者具体滑动操作的时候也很慢. 二.卡顿的原因 卡顿的 ...

  10. 用Phaser来制作一个html5游戏——flappy bird (一)

    Phaser是一个简单易用且功能强大的html5游戏框架,利用它可以很轻松的开发出一个html5游戏.在这篇文章中我就教大家如何用Phaser来制作一个前段时间很火爆的游戏:Flappy Bird,希 ...

随机推荐

  1. vue自定义全局组件(自定义插件)

    有时候我们在做开发的时候,就想自己写一个插件然后就可以使用自己的插件,那种成就感很强.博主最近研究element-ui和axios的时候,发现他们是自定义组件,但是唯一有一点不同的是,在用elemen ...

  2. 15 Python 迭代器和生成器

    什么是迭代 (iterable) 字符串.列表.元组.字典.集合都可以被for循环,说明他们都是可迭代的. 可以直接作用于for循环的对象统称为可迭代对象(Iterable). 可以被next()函数 ...

  3. linux 下安装rar解压

    在liunx下原本是不支持rar文件的,需要安装liunx下的winrar版本,操作如下 wget http://www.rarsoft.com/rar/rarlinux-4.0.1.tar.gz t ...

  4. Brackets (区间DP)

    个人心得:今天就做了这些区间DP,这一题开始想用最长子序列那些套路的,后面发现不满足无后效性的问题,即(,)的配对 对结果有一定的影响,后面想着就用上一题的思想就慢慢的从小一步一步递增,后面想着越来越 ...

  5. 洛谷 P1655 小朋友的球

    题目描述 @发源于 小朋友最近特别喜欢球.有一天他脑子抽了,从口袋里拿出了N个不同的球,想把它们放到M个相同的盒子里,并且要求每个盒子中至少要有一个球,他好奇有几种放法,于是尝试编程实现,但由于他天天 ...

  6. 图的m着色问题 (回溯搜索)

    图的m着色问题 [问题描述]        给定无向连通图G和m种不同的颜色.用这些颜色为图G的各顶点着色,每个顶点着一种颜色.如果有一种着色法使G中每条边的2个顶点着不同颜色,则称这个图是m可着色的 ...

  7. 横向排列两个多个div盒子的方法(CSS浮动清除float-clear/inline)/办法

    最近在做一个div css切割,昨晚发现了长期以来一直无记录下来的问题!关于兼容IE跟FF的float属性.趁现在还清醒赶紧记下笔记先:一.并排在一行的两个div样式有这种情况:ie或者ff下对于子d ...

  8. Linux查看物理CPU个数、核数,逻辑CPU个数

    学习swoole的时候,建议开启的worker进程数为cpu核数的1-4倍.于是就学习怎么查看CPU核数 # 查看物理CPU个数 cat /proc/cpuinfo| grep "physi ...

  9. GWT 中实现“CSS Sprite”

    近段时间在弄GWT这一块,开发中遇到的一些不错的方法或者技巧,在此做个分享和记录,有不同见解可发表意见  互相切磋. 在web开发中,必然涉及到网页中的图片,本地浏览网页,要下载在服务器端的图片,然后 ...

  10. 无法删除image报rbd: error: image still has watchers解决方法

    标签(空格分隔): ceph,ceph运维,rbd 解决思路: 在Ceph集群日常运维中,管理员可能会遇到有的image删除不了的情况: 1) 由于image下有快照信息,只需要先将快照信息清除,然后 ...