24point

 ACM  Search 󰈭 555字

24点小游戏~

cpp
  1#include <iostream>
  2      
  3#include <algorithm>
  4#include <string>
  5#include <vector>
  6#include <stack>
  7#include <queue>
  8#include <set>
  9#include <map>
 10#include <unordered_map>
 11#include <unordered_set>
 12#include <random>
 13#include <chrono>
 14     
 15#include <cstdio>
 16#include <cstring>
 17#include <cmath>
 18#include <ctime>
 19#include <cstdlib>
 20#include <cassert>
 21      
 22#define itn int
 23#define fro for
 24#define scnaf scanf
 25#define sacnf scanf
 26#define manx maxn
 27#define pritnf printf
 28
 29#define Fastin freopen("in.txt", "r", stdin)
 30#define Fastout freopen("out.txt", "w", stdout)
 31#define min(a, b) ((a) < (b)? (a): (b))
 32#define max(a, b) ((a) > (b)? (a): (b))
 33#define lowbit(x) ((x) & (-x))
 34#define ceil(n, p) (((n) +(p) - 1) / (p))
 35#define DBG(x) (void)(cout << "L" << __LINE__ << ": " << #x << " = " <<( x) << '\n')
 36#define clr(a, x) memset((a), x, sizeof (a))
 37#define TTT int __; scanf("%d", &__); while(__--)
 38using namespace std;
 39typedef long long ll;
 40
 41int a[5], b[5], p[5];
 42
 43struct NODE{
 44    string s; double w;
 45    bool operator < (const NODE& b)const{return s < b.s;}
 46};
 47set<NODE> s[5][5];
 48//处理[left, right]的值
 49void sove(int left, int right){
 50    if(left > right) return ;
 51    if(left == right){
 52        NODE temp;
 53        temp.s = to_string(a[left]); temp.w = a[left];
 54        s[left][right].insert(temp);
 55        return;
 56    }
 57    for(int i = left; i <= right - 1; i++){
 58        sove(left, i); sove(i + 1, right);
 59        NODE temp;
 60        for(auto x: s[left][i]) for(auto y: s[i + 1][right]){
 61            temp.s = x.s; if(left != i) temp.s = "(" + temp.s + ")";
 62            temp.s += "+"; temp.w = x.w + y.w;
 63            if(i + 1 != right) temp.s += "(" + y.s + ")";
 64            else temp.s += y.s;
 65            s[left][right].insert(temp);
 66            
 67            temp.s = x.s; if(left != i) temp.s = "(" + temp.s + ")";
 68            temp.s += "-"; temp.w = x.w - y.w;
 69            if(i + 1 != right) temp.s += "(" + y.s + ")";
 70            else temp.s += y.s;
 71            s[left][right].insert(temp);
 72            
 73            temp.s = x.s; if(left != i) temp.s = "(" + temp.s + ")";
 74            temp.s += "*"; temp.w = x.w * y.w;
 75            if(i + 1 != right) temp.s += "(" + y.s + ")";
 76            else temp.s += y.s;
 77            s[left][right].insert(temp);
 78            
 79            if(y.w == 0) continue;
 80            temp.s = x.s; if(left != i) temp.s = "(" + temp.s + ")";
 81            temp.s += "/"; temp.w = x.w / y.w;
 82            if(i + 1 != right) temp.s += "(" + y.s + ")";
 83            else temp.s += y.s;
 84            s[left][right].insert(temp);
 85        }
 86        
 87    }
 88}
 89
 90const double eps = 1e-6;
 91set<string> ans;
 92
 93int main(){
 94//    Fastin;
 95    for(int i = 1; i <= 4; i++) scnaf("%d", b + i);
 96    p[1] = 1; p[2] = 2; p[3] = 3; p[4] = 4;
 97    
 98    do{
 99        for(int i = 0; i < 5; i++) for(int j = 0; j < 5; j++) s[i][j].clear();
100        for(int i = 1; i <= 4; i++) a[i] = b[p[i]];
101        sove(1, 4);
102        for(auto x: s[1][4]) if(abs(x.w - 24) <= eps){
103            ans.insert(x.s);
104        }
105    }while(next_permutation(p + 1, p + 5));
106    
107    if(ans.empty()) puts("-1");
108    else for(auto s: ans) cout << s << '\n';
109    return 0;
110}
嗨! 这里是 rqdmap 的个人博客, 我正关注 GNU/Linux 桌面系统, Linux 内核 以及一切有趣的计算机技术! 希望我的内容能对你有所帮助~
如果你遇到了任何问题, 包括但不限于: 博客内容说明不清楚或错误; 样式版面混乱; 加密博客访问请求等问题, 请通过邮箱 rqdmap@gmail.com 联系我!
修改日志
  • 2023-09-01 18:14:49 单独划分ACM专题; 移动部分博客进入黑洞归档
  • 2023-05-29 23:05:14 博客结构与操作脚本重构
  • 2023-05-08 21:44:36 博客架构修改升级
  • 2022-11-16 01:27:34 迁移老博客文章内容