当前位置: 首页 > news >正文

csu 1600: Twenty-four point

传送门

 

1600: Twenty-four point

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 490  Solved: 78
[Submit][Status][Web Board]

Description

Given four numbers, can you get twenty-four through the addition, subtraction, multiplication, and division? Each number can be used only once.

 

Input

The input consists of multiple test cases. Each test case contains 4 integers A, B, C, D in a single line (1 <= A, B, C, D <= 13).

 

Output

For each case, print the “Yes” or “No”. If twenty-four point can be get, print “Yes”, otherwise, print “No”.

 

Sample Input

2 2 3 9
1 1 1 1 
5 5 5 1

Sample Output

Yes
No
Yes

HINT

 

For the first sample, (2/3+2)*9=24.


 

 

Source

 
思路:
dfs,把数组当参数,4个数先选2个算一下,然后变成三个数,传入当下一层,避免了麻烦的选数
 
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <stack>
 6 #include <cctype>
 7 #include <vector>
 8 #include <cmath>
 9 #include <map>
10 #include <queue>
11 
12 #define ll long long
13 #define eps 1e-8
14 
15 using namespace std;
16 
17 double a[5];
18 
19 int dfs(double p[],int len)
20 {
21     if(len==1){
22         if(fabs(p[0]-24)<eps){
23             return 1;
24         }
25         else{
26             return 0;
27         }
28     }
29     double f[5];
30     int cou;
31     int i,j,k;
32     for(i = 0;i <len-1;i++){
33         for(j=i+1;j<len;j++){
34             cou=0;
35             for(k=0;k<len;k++){
36                 if(k!=i && k!=j){
37                     f[cou]=p[k];cou++;
38                 }
39             }
40             f[cou]=p[i]+p[j];
41             if(dfs(f,cou+1)) return 1;
42             f[cou]=p[i]-p[j];
43             if(dfs(f,cou+1)) return 1;
44             f[cou]=p[j]-p[i];
45             if(dfs(f,cou+1)) return 1;
46             f[cou]=p[i]*p[j];
47             if(dfs(f,cou+1)) return 1;
48             if(p[j]!=0){
49                 f[cou]=p[i]/p[j];
50                 if(dfs(f,cou+1)) return 1;
51             }
52             if(p[i]!=0){
53                 f[cou]=p[j]/p[i];
54                 if(dfs(f,cou+1)) return 1;
55             }
56         }
57     }
58     return 0;
59 }
60 
61 int main()
62 {
63     //freopen("in.txt","r",stdin);
64     //scanf("%d",&T);
65     //for(int ccnt=1;ccnt<=T;ccnt++){
66     while(scanf("%lf%lf%lf%lf",&a[0],&a[1],&a[2],&a[3])!=EOF){
67         if(dfs(a,4) == 1){
68             printf("Yes\n");
69         }
70         else{
71             printf("No\n");
72         }
73     }
74     return 0;
75 }

 

转载于:https://www.cnblogs.com/njczy2010/p/5246590.html

相关文章:

  • 常用的Linux操作命令(一)
  • 2014103《JAVA程序设计》第一周学习总结
  • 2月第4周网络安全报告:境内69.1万主机感染病毒
  • 本地环境,Ecshop安装教程
  • 使用maven+eclipse构建spark项目
  • pull解析和sax解析的差别
  • eclipse中JS文件乱码
  • JavaScript设计模式与开发实践系列之策略模式
  • 颜色名列表
  • 设计模式简介
  • 获取坐标封装 getPos
  • 修改vsftp默认端口
  • struts2标签报错问题
  • freeipa
  • cordova plugin数据传递概要
  • [分享]iOS开发-关于在xcode中引用文件夹右边出现问号的解决办法
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • 【刷算法】从上往下打印二叉树
  • 03Go 类型总结
  • Cumulo 的 ClojureScript 模块已经成型
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • gcc介绍及安装
  • Median of Two Sorted Arrays
  • mysql常用命令汇总
  • springMvc学习笔记(2)
  • vue2.0开发聊天程序(四) 完整体验一次Vue开发(下)
  • vue的全局变量和全局拦截请求器
  • WePY 在小程序性能调优上做出的探究
  • Xmanager 远程桌面 CentOS 7
  • 复杂数据处理
  • 关于for循环的简单归纳
  • 全栈开发——Linux
  • 数组的操作
  • 项目管理碎碎念系列之一:干系人管理
  • 如何正确理解,内页权重高于首页?
  • ![CDATA[ ]] 是什么东东
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • (4)(4.6) Triducer
  • (day 2)JavaScript学习笔记(基础之变量、常量和注释)
  • (Python) SOAP Web Service (HTTP POST)
  • (ZT)出版业改革:该死的死,该生的生
  • (附源码)springboot猪场管理系统 毕业设计 160901
  • (三维重建学习)已有位姿放入colmap和3D Gaussian Splatting训练
  • (转)http协议
  • (转)visual stdio 书签功能介绍
  • (转)大型网站架构演变和知识体系
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .NET 的静态构造函数是否线程安全?答案是肯定的!
  • .net 使用$.ajax实现从前台调用后台方法(包含静态方法和非静态方法调用)
  • .NET 中让 Task 支持带超时的异步等待
  • .net图片验证码生成、点击刷新及验证输入是否正确
  • .Net中间语言BeforeFieldInit
  • .sys文件乱码_python vscode输出乱码
  • .考试倒计时43天!来提分啦!