34- 24 Point game
http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=43
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 - 来源
- 经典改编
- 上传者
- 张云聪
-
#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的更多相关文章
- /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 ...
- java处理中国气象数据,提取汇总陕西地区24小时各观测点的数据(csv格式)
1.先贴一下气象数据的csv源格式,由于数据内容较多,就放一部分(china_sites_20150102.csv) date,hour,type,1001A,1002A,1003A,1004A,10 ...
- 求N个数的最大公约数和最小公倍数(转)
除了分解质因数,还有另一种适用于求几个较小数的最大公约数.最小公倍数的方法 下面是数学证明及算法实现 令[a1,a2,..,an] 表示a1,a2,..,an的最小公倍数,(a1,a2,..,an)表 ...
- Linux 磁盘自检介绍
在Linux系统中,有时候重启会耗费非常长的时间,如果你进一步检查细节,就会发现绝大部分时间都耗费在磁盘自检(fsck)上了,有时候遇到时间比较紧急的情况,磁盘自检耗费的时间非常长,真的是让人心焦火急 ...
- 软件工程(FZU2015)赛季得分榜,第11回合(beta冲刺+SE总结)
目录 第一回合 第二回合 第三回合 第四回合 第五回合 第6回合 第7回合 第8回合 第9回合 第10回合 第11回合 增补作业 积分规则 积分制: 作业为10分制,练习为3分制:alpha30分:b ...
- aes加密C语言
/** * \file aes.h * * \brief AES block cipher * * Copyright (C) 2006-2010, Brainspark B.V. * * This ...
- ZAM 3D 制作简单的3D字幕 流程(二)
原地址:http://www.cnblogs.com/yk250/p/5663907.html 文中表述仅为本人理解,若有偏差和错误请指正! 接着 ZAM 3D 制作简单的3D字幕 流程(一) .本篇 ...
- GCD、dispatch函数介绍
iOS多线程的方法有3种: NSThread NSOperation GCD(Grand Central Dispatch) 其中,由苹果所倡导的为多核的并行运算提出的解决方案:GCD能够访问线程池, ...
- 【Android端 APP GPU过度绘制】GPU过度绘制及优化
一.Android端的卡顿 Android端APP在具体使用的过程中容易出现卡顿的情况,比如查看页面时出现一顿一顿的感受,切换tab之后响应很慢,或者具体滑动操作的时候也很慢. 二.卡顿的原因 卡顿的 ...
- 用Phaser来制作一个html5游戏——flappy bird (一)
Phaser是一个简单易用且功能强大的html5游戏框架,利用它可以很轻松的开发出一个html5游戏.在这篇文章中我就教大家如何用Phaser来制作一个前段时间很火爆的游戏:Flappy Bird,希 ...
随机推荐
- php 中改变字符编码的函数 是 iconv()
json_enocode() 此函数里边接收的数据必须是utf8格式.要不然会输出null
- [转载] 最简单的基于FFmpeg的AVDevice例子(读取摄像头)
=====================================================最简单的基于FFmpeg的AVDevice例子文章列表: 最简单的基于FFmpeg的AVDev ...
- Linux中sort和uniq关于排序去重的那些用法
相信在Linux下对文件操作经常会用到sort和uniq命令,下面系统的介绍一下这两个命令的用法. sort命令是在Linux里非常有用,它将文件进行排序,并将排序结果标准输出.sort命令既可以从特 ...
- poj1463 Strategic game[树形DP]
求一棵树每条边都被选上的点覆盖掉的最少选点数. 一条边被覆盖掉,必须他父亲和儿子中选一个..这不就是比NOIP2018D2T3还裸的暴力么.水掉. lyd给的练习题都什么**玩意儿.. code不挂了 ...
- jsp有哪些动作?作用分别是什么?
jsp共有6种基本动作: 1.jsp:include,在页面被请求的时候引入一个文件 2.jsp:useBean,寻找或者实例化一个JavaBean 3.jsp:setProperty,设置JavaB ...
- es6字符串的扩展学习笔记
1. 传统上,JavaScript只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6又提供了三种新方法. includes():返回布尔值,表示是否找到了参数字符串. st ...
- 推荐几个MySQL大牛的博客
1.淘宝丁奇 http://dinglin.iteye.com/ 2.周振兴@淘宝 花名:苏普 http://www.orczhou.com/ 3. 阿里云数据库高级专家彭立勋为 MariaDB Fo ...
- 自定义mysql函数时报错,[Err] 1418 - This function has none of DETERMINISTIC......
今天在我执行自定义mysql函数的SQL时发生了错误,SQL如下: /** 自定义mysql函数 getChildList */delimiter //CREATE FUNCTION `pengwif ...
- POJ2536(二分图最大匹配)
Gopher II Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8504 Accepted: 3515 Descrip ...
- 西安电子科技大学第16届程序设计竞赛 B Words Game
链接:https://www.nowcoder.com/acm/contest/107/B来源:牛客网 Words Game 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 13107 ...