• 华为机试题 24点运算


    简介

    使用回溯 + 暴力

    code

    #include <iostream>
    #include <string>
    #include <map>
    #include <vector>
    using namespace std;
    bool flag = false;
    char v[4]={'+', '-', '*', '/'};
    string mm[14] = {"0", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    int calc(char x, int a, int b){
        switch(x){
            case '+': return a+b;
            case '-': return a-b;
            case '*': return a*b;
            case '/': return b == 0 ? 1111111 : a/b;
        }
    }
    bool check(vector<int> &path){
        for(int j=0; j<4; j++){
            int ee = calc(v[j], path[0], path[1]);
            if(ee == 1111111) continue;
            for(int k=0; k<4; k++) {
                int ff = calc(v[k], ee, path[2]);
                if(ff == 1111111) continue;
                for(int l=0; l<4; l++){
                    int gg = calc(v[l], ff, path[3]);
                    if(gg == 1111111) continue;
                    if(gg == 24) {
                        cout << mm[path[0]] << v[j] << mm[path[1]] << v[k] << mm[path[2]] << v[l] <<
                             mm[path[3]] << endl; 
                        return true;
                    }
                }
            }
        }
        return false;
    }
    void dfs(vector<int> &v, map<int, bool> &vis, vector<int> &path){
        if(path.size() == v.size()) {
            flag = check(path);
            return;
        }
        for(int i=0; i<4; i++){
            if(flag) {
                return;
            }
            if(vis[i] == false) {
                vis[i] = true;
                path.push_back(v[i]);
                dfs(v, vis, path);
                if(flag) {
                    return;
                }
                path.pop_back();
                vis[i] = false;
            }
        }
    }
    int main() {
        string a, b, c, d;
        map<string, int> m;
        m["2"] = 2;
        m["3"] = 3;
        m["4"] = 4;
        m["5"] = 5;
        m["6"] = 6;
        m["7"] = 7;
        m["8"] = 8;
        m["9"] = 9;
        m["10"] = 10;
        m["A"] = 1;
        m["J"] = 11;
        m["Q"] = 12;
        m["K"] = 13;
    
        int aa, bb, cc, dd;
        
        while(cin >> a >> b >> c >> d) {
            if(a == "joker" || b == "joker" || c == "joker" || d=="joker") {
                cout << "ERROR
    ";
                continue;
            }
            vector<int> vv(4);
            vv[0] = m[a];
            vv[1] = m[b];
            vv[2] = m[c];
            vv[3] = m[d];
            map<int, bool> vis;
            vector<int> path;
            dfs(vv, vis, path);
            if(!flag) cout << "NONE
    ";
        }
        
    }
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    Blur Detection using OpenCV (Laplacian and FFT based approach)
    iBlurDetect: Image Blur Detection Techniques Assessment and Evaluation Study
    Cassandra
    ClickHouse
    xarray 生成 tiff 文件
    PostgreSQL 安装与使用
    PostgreSql和PostGIS安装——Windows10家庭版
    python 安装geopandas
    Linux从基础到架构师学习路线 摩天居士
    10 Python编程:从入门到实践socket&socketserver 摩天居士
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/14940634.html
Copyright © 2020-2023  润新知