34- 24 Point game
2024-10-18 12:58:19
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;
}
最新文章
- ffmpeg将图片合成视频
- 关于HashTable的遍历方法解析
- caffe学习系列(6):其他层介绍
- android中判断网络连接是否可用
- LDO-XC6216C202MR-G
- [Aaronyang] 写给自己的WPF4.5 笔记24 [与winform交互-flash-DEMO-收尾篇1/6]
- LayoutParams使用
- ZOJ1586——QS Network(最小生成树)
- 【转】JSONP简介
- php中utf8 与utf-8
- java日期工具类(Long型,Date型,yyyyMMdd型)等
- Alluxio 1.5集群搭建
- [原创]MinHook测试与分析(x64下 E9,EB,CALL指令测试,且逆推测试微软热补丁)
- 获取目录-Winform
- HTML中的Hack条件注释语句
- django xadmin(1)
- MVC架构在Asp.net中的应用和实现
- C#基础篇十小练习
- 我的CSS命名规则
- IntelliJ IDEA遇到Unable to parse template “Class”错误
热门文章
- Linux 性能监控分析
- 非root用户 如何将cscope安装到指定目录,vim74安装
- Unity 5 官方打包管理工具 Asset Bundle Manager
- C# partial 说明(转)
- Linux下markdown编辑软件 — retext 支持实时预览,存为pdf、html、ODT等
- poj 2096 , zoj 3329 , hdu 4035 —— 期望DP
- 第一章计算机网络和因特网-day02
- 免费的xshell下载
- commandLink/commandButton/ajax backing bean action/listener method not invoked (转)
- 蓝桥杯 算法训练 ALGO-147 4-3水仙花数