帖一个自己写的,VS能输出24点全部组合的代码。

#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<cmath>
#include<unordered_map>
#include<algorithm>
#include<stack>
using namespace std;



vector<string> table = { "#","A","2","3","4","5","6","7","8","9","10","J","Q","K","joker","JOKER" };
string op_tb = "+-*/";

string temp = "";
vector<string> ans;
double res;

vector<vector<int>> r;
vector<int> t;



void dfs(const vector<int>& nums, char op, int flag) {
    if (flag > 3) {
        if (res == 24 && find(ans.begin(), ans.end(), temp.substr(0, temp.size() - 1)) == ans.end())
            ans.push_back(temp.substr(0, temp.size() - 1));
        return;
    }
    if (flag == 0)
        res = nums[flag];
    else if (op == '+')
        res += nums[flag];
    else if (op == '-')
        res -= nums[flag];
    else if (op == '*')
        res *= nums[flag];
    else if (op == '/')
        res /= nums[flag];
    else
        cout << "NOPE OP" << endl;
    double d = res;

    temp += table[nums[flag]];
    string s = temp;

    for (int i = 0; i < 4; i++)
    {
        temp += op_tb[i];
        dfs(nums, op_tb[i], flag + 1);
        res = d;
        temp = s;
    }
}

void backtrack(const vector<int>& nums)
{
    if (t.size() == nums.size()) {
        r.push_back(t);
        return;
    }
    for (int i = 0; i < nums.size(); i++) {
        if (find(t.begin(), t.end(), nums[i]) != t.end())
            continue;
        t.push_back(nums[i]);
        backtrack(nums);
        t.pop_back();
    }
}

int main()
{
    while (1) {
        temp.clear(); ans.clear(); t.clear(); r.clear(); bool ff = false;
        cout << "Input 4 cards: " << endl;
        vector<int> nums(4);
        for (int i = 0; i < 4; i++) {
            string s;
            cin >> s;
            if (s == "joker" || s == "JOKER")
            {
                ff = true; cout << "Contains joker, error!" << endl;
            }
            else if (find(table.begin(), table.end(), s) != table.end())
                nums[i] = find(table.begin(), table.end(), s) - table.begin();
            else
                cout << "Wrong input!" << endl << "----------------" << endl;                
        }
        if (ff)
            continue;
        vector<int> nums_1 = { 0,1,2,3 };
        backtrack(nums_1);
        for (int i = 0; i < r.size(); i++)
        {
            for (int j = 0; j < r[i].size(); j++)
                r[i][j] = nums[r[i][j]];
        }
        for (auto e : r) {
            temp.clear();
            dfs(e, ' ', 0);
        }
        if (ans.size() == 0)
            cout << "Can not sum in 24!" << endl << "-------------" << endl;
        else {
            cout << "Cards can be combined into 24 as follows: " << endl;
            for (auto e : ans)
            {
                for (int j = 0; j < e.size(); j++)
                    cout << e[j];
                cout << endl;
            }
            cout << "-------------" << endl;
        }
    }
    system("pause");
    return 0;
}