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. Nhibernate Fluent INNER JOIN 查询

    var list = session.QueryOver<PluginEntity>().JoinQueryOver(o => o.PluginModule, NHibernate. ...

  2. SpringMVC_总结_03_SpringMVC相关注解

    一.前言 在前面的小节中,我们配置了注解驱动和自动扫描包,然后就可以使用SpringMVC相关注解了. 二.@Controller @Controller用来修饰类,源码如下: package org ...

  3. Linux-压缩与解压缩命令

    常用的压缩格式:.zip   .gz   .bz2   .tar.gz   .tar.bz2 1.Zip 压缩文件:zip 压缩文件名  源文件 压缩目录:zip -r 压缩文件名 源文件 解压缩.z ...

  4. html5视频video积累

    又是好几个月没有写东西,还是太懒散了~必须要教育下自己罗~ 这次做了个播放视频的移动H5,之前没有仔细玩过,好好记录下基本知识,还有遇到的一些坑,方便之后再次遇见后进行解决 一.基本 video标签在 ...

  5. nginx中关于并发数的问题worker_connections,worker_processes

    我认为,要搞清楚这个公式是否正确,以及如何计算的,那首先要对nginx的各个配置说明有清晰的认识: 从用户的角度,http 1.1协议下,由于浏览器默认使用两个并发连接,因此计算方法: nginx作为 ...

  6. Android源代码因删除所有git仓库导致的编译错误

    /******************************************************************************** * Android源代码因删除所有g ...

  7. 总结的一些MySQL数据库面试题

    1.sql语句应该考虑哪些安全性? 1.防止sql注入,对特殊字符进行转义,过滤或者使用预编译的sql语句绑定变量. 2.最小权限原则,特别是不要用root账户,为不同的类型的动作或者组建使用不同的账 ...

  8. swing之复杂登陆界面的实现

    package jiemian; import gonggong.message; import gonggong.messageType; import gonggong.user; import ...

  9. 关于yii的日志路由组件的配置问题

    最近突然意识到日志是很好滴debug工具,所以研究了一下yii的日志配置,想想应该还会有像我这样的小白不懂这些问题的,就分享一下了.有错误烦请大神们指出config/main.php 中配置,这个想必 ...

  10. 学习动态性能表(13)--v$open_cursor

    学习动态性能表 第13篇--V$OPEN_CURSOR  2007.6.8 本视图列出session打开的所有cursors,很多时候都将被用到,比如:你可以通过它查看各个session打开的curs ...