HNU 12886 Cracking the Safe 二十四点的判断

经典的一个题,今天竟然写跪了……

题意:

给你4个数字,让你判断是否能通过四则运算和括号,凑成24点。

思路:

暴力枚举运算顺序和运算符。

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <algorithm>
 7 #include <string>
 8 #include <queue>
 9 #include <stack>
10 #include <vector>
11 #include <map>
12 #include <set>
13 #include <functional>
14 #include <time.h>
15
16 using namespace std;
17
18 const int INF = 1<<30;
19 const double eps = 1e-6;
20
21 double a[4];
22 inline double myAbs(double x) {
23     return x>0 ? x : -x;
24 }
25
26 bool dfs(double S[], int cnt) { //S[]中存放当前还有的数字
27 //计算方法: 每次从剩下的数字里取出两个数运算,相当于枚举运算顺序
28     if (cnt==1) //最后一个数字
29         return myAbs(24-S[0])<eps;
30
31     double b[4];
32     for (int i = 0; i < cnt; i++) //枚举两个运算的数字
33         for (int j = 0; j < cnt; j++) if (i!=j) {
34             for (int k = 0, p = 1; k < cnt; k++) {
35                 if (k!=i&&k!=j)
36                     b[p++] = S[k];
37             }
38             //枚举运算
39             b[0] = S[i]+S[j];
40             if (dfs(b, cnt-1)) return true;
41             b[0] = S[i]-S[j];
42             if (dfs(b, cnt-1)) return true;
43             b[0] = S[i]*S[j];
44             if (dfs(b, cnt-1)) return true;
45             b[0] = S[i]/S[j];
46             if (dfs(b, cnt-1)) return true;
47         }
48     return false;
49 }
50
51 void solve() {
52     do { //枚举排列
53         if (dfs(a, 4)) {
54             puts("YES");
55             return ;
56         }
57     }while (next_permutation(a, a+4));
58     puts("NO");
59 }
60
61 int main() {
62     #ifdef Phantom01
63         freopen("HNU12879.txt", "r", stdin);
64     #endif //Phantom01
65
66     int T;
67     scanf("%d", &T);
68     while (T--) {
69         for (int i = 0; i < 4; i++)
70             scanf("%lf", &a[i]);
71         solve();
72     }
73
74     return 0;
75 }

HNU 12886 Cracking the Safe 二十四点的判断,布布扣,bubuko.com

时间: 2024-08-25 14:35:32

HNU 12886 Cracking the Safe 二十四点的判断的相关文章

HNU 12886 Cracking the Safe(暴力枚举)

题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数,要你判断用 + .- . * ./.四种运算能不能得到一个结果为24的式子,可以用括号. 解释一下测试的第四组样例:应该是6 / (1 - 3 / 4) 暴力枚举三种符号分别是什么,然后枚举这三种符号运算的顺序,然后枚举这四个数字的24种排列方式,时间是4^3 * 6 * 24 然后注意要用double型,

Scala二十四点游戏

Scala二十四点游戏(1):表达式计算(一) Scala二十四点游戏(2):表达式计算(二) Scala二十四点游戏(3):表达式计算(三) Scala二十四点游戏(4):算法之一 Scala二十四点游戏(5):List简介 Scala二十四点游戏(6):实现全排列 Scala二十四点游戏(7):穷举可能的表达式 Scala二十四点游戏(8): 计算24的算法 Scala二十四点游戏(9): 完整的代码和计算结果 Scala二十四点游戏(10): 更简单的表达式算法 Scala二十四点游戏(1

二十四点问题

判断四个数是否可能通过加减乘得到二十四, 可以使用括号,思路极其像Expression Add Operators, 区别是在处理乘号的时候,一种是没有括号的,和Expression Add Operators一样,一种是带有括号的,其实就是把乘号当作加号来计算 public class Solution { public boolean solution(List<Integer> list) { return helper(list, 0); } public boolean helper

Java从零开始学二十四点(集合工具类Collections)

一.Collections简介 在集合的应用开发中,集合的若干接口和若干个子类是最最常使用的,但是在JDK中提供了一种集合操作的工具类 —— Collections,可以直接通过此类方便的操作集合 二.Collections类的常用方法及常量 No. 方法 类型 描述 1 public static final List EMPTY_LIST 常量 返回一个空的List集合 2 public static final Set EMPTY_SET 常量 返回空的Set集合 3 public sta

二十四点游戏

#include<iostream> #include<string.h> #include<string> #include<stack> using namespace std; bool lessthan(char a, char b) { if(b=='*' || b=='/' ) { return true; } if(a=='+' || a=='-' ) { return true; } return false; } void compute(

[LeetCode] 24 Game 二十四点游戏

You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *, /, +, -, (, )to get the value of 24. Example 1: Input: [4, 1, 8, 7] Output: True Explanation: (8-4) * (7-1) = 24 Example 2: Input: [1, 2,

二十四点

本来想用后缀表达式,但是感觉代码太长了,算式也不复杂就懒得写,就模拟了下 过程,思路很简单,算两趟,第一次算乘除,第二次算加减 import java.util.LinkedList; import java.util.Scanner; /** * @Auther: Pengwen * @Date: 2019/12/8 21:29 * @Description: */ public class Main { public static void main(String[] args) { Sca

CSP201903-2二十四点

如图所示先处理乘号和除号,再处理加减. #include<bits/stdc++.h> using namespace std; bool res[101];int main(){ int n; cin>>n; int i,j,op1,op2; string inp; char op[3]; int nn[4]; int sum,nnum=0; int opnum=0; int nns[4]; char ops[3]; for(i=0;i<n;i++){ cin>>

201903-2 二十四点

n = int(input())result = []for i in range(n): temp = str(input()) if("x" in temp): temp = temp.replace("x","*") if("/" in temp): temp = temp.replace("/","//") if(eval(temp) == 24): result.append(