参考了解答,用枚举遍历的方式实现。
#include <bits/stdc++.h>
using namespace std;

double cal(double a, double b, char ch) {
	switch (ch) {
	case '+':
		return a + b;
	case '-':
		return a - b;
	case '*':
		return a * b;
	case '/':
		return a / b;
	}
    return 0;
}

bool check(vector<int> &nums) {
    char op[4] = {'+','-','*','/'};
    sort(nums.begin(), nums.end());
    do {
        // 遍历三个位置所有可能的
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                for (int k = 0; k < 4; k++) {
                    double first = cal(nums[0], nums[1], op[i]);
                    double second = cal(first, nums[2], op[j]);
                    if (cal(second, nums[3], op[k]) == 24) return true;
                }
            }
        }
    } while (next_permutation(nums.begin(), nums.end()));
    return false;
}

int main() {
    vector<int> nums;
    for (int i = 0; i < 4; i++) {
        int temp = 0;
        cin >> temp;
        nums.push_back(temp);
    }
    if (check(nums)) { cout << "true" << endl; }
    else { cout << "false" << endl; }
    return 0;
}