C++24点

我玩24点总是输发现有一些神秘解法很难想(例如7 7 2 1 (7*7-1)/2=24),信息老师又让我做24点,于是我就做了:

#include<bits/stdc++.h>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

const int TARGET = 24;
const double EPSILON = 1e-6;
map<string,int>mp;
// 判断是否需要添加括号
bool needParentheses(const string& parentOp, const string& currentOp) {
    // 如果父级运算符是 '+' 或 '-',且当前运算符是 '*' 或 '/',则需要括号
    if ((parentOp == "+" || parentOp == "-") && (currentOp == "*" || currentOp == "/")) {
        return true;
    }
    return false;
}

bool needParentheses2(const string& parentOp, const string& currentOp) {
    // 如果父级运算符是 '+' 或 '-',且当前运算符是 '*' 或 '/',则需要括号
    if (((parentOp == "+"||parentOp == "-") && currentOp == "-") || ((parentOp == "*" ||parentOp == "/")&& currentOp == "/")) {
        return true;
    }
    return false;
}
bool solve2(vector<int>& nums, vector<string>& expr, vector<string>& ops, string& result) {
	int f=0;
	if (nums.size() == 1) {
        if (fabs(nums[0] - TARGET) < EPSILON) {
        	cout << expr[0] << endl;
            return true;
        }
        return false;
    }

    for (size_t i = 0; i < nums.size(); ++i) {
        for (size_t j = 0; j < nums.size(); ++j) {
            if (i == j) continue;

            vector<int> nextNums;
            vector<string> nextExpr;
            vector<string> nextOps;
            for (size_t k = 0; k < nums.size(); ++k) {
                if (k != i && k != j) {
                    nextNums.push_back(nums[k]);
                    nextExpr.push_back(expr[k]);
                    nextOps.push_back(ops[k]);
                }
            }

            // Try addition
            string newExpr="";
            if(i<j){
	            newExpr="";
	            if(needParentheses(ops[i], "+") ) {
	            	newExpr+=("("+expr[i]+")");
				}
				else{
					newExpr+=expr[i];
				}
	            newExpr += "+";
	           	if(needParentheses(ops[j], "+") ) {
	           		newExpr+=("("+expr[j]+")");
				}
				else{
					newExpr+=expr[j];
				}
				nextNums.push_back(nums[i] + nums[j]);
	            nextExpr.push_back(newExpr);
	            nextOps.push_back("+");
	            if (solve2(nextNums, nextExpr, nextOps, result)) {
	                f=1;
	            }
	            nextNums.pop_back();
	            nextExpr.pop_back();
	            nextOps.pop_back();
			}
            // Try subtraction
            if(nums[i]>=nums[j]){
	            newExpr="";
	            if(needParentheses(ops[i], "-") ) {
	            	newExpr+=("("+expr[i]+")");
				}
				else{
					newExpr+=expr[i];
				}
	            newExpr += "-";;
	           	if(needParentheses(ops[j], "-") || needParentheses2(ops[j], "-")) {
	           		newExpr+=("("+expr[j]+")");
				}
				else{
					newExpr+=expr[j];
				}
	            nextNums.push_back(nums[i] - nums[j]);
	            nextExpr.push_back(newExpr);
	            nextOps.push_back("-");
	            if (solve2(nextNums, nextExpr, nextOps, result)) {
	                f=1;
	            }
	            nextNums.pop_back();
	            nextExpr.pop_back();
	            nextOps.pop_back();
			}
			
            // Try multiplication
            if(i<j){
	            newExpr="";
	            if(needParentheses(ops[i], "*") ) {
	            	newExpr+=("("+expr[i]+")");
				}
				else{
					newExpr+=expr[i];
				}
	            newExpr += "*";;
	           	if(needParentheses(ops[j], "*") ) {
	           		newExpr+=("("+expr[j]+")");
				}
				else{
					newExpr+=expr[j];
				}
	            nextNums.push_back(nums[i] * nums[j]);
	            nextExpr.push_back(newExpr);
	            nextOps.push_back("*");
	            if (solve2(nextNums, nextExpr, nextOps, result)) {
	                 f=1;
	            }
	            nextNums.pop_back();
	            nextExpr.pop_back();
	            nextOps.pop_back();
	        }

            // Try division
            if (nums[j] != 0 && nums[i] % nums[j] == 0) {
            	newExpr="";
            	if(needParentheses(ops[i], "/") ) {
            		newExpr+=("("+expr[i]+")");
				}
				else{
					newExpr+=expr[i];
				}
                newExpr += "/";;
            	if(needParentheses(ops[j], "/") || needParentheses2(ops[j], "/")) {
            		newExpr+=("("+expr[j]+")");
				}
				else{
					newExpr+=expr[j];
				}
                nextNums.push_back(nums[i] / nums[j]);
                nextExpr.push_back(newExpr);
                nextOps.push_back("/");
                if (solve2(nextNums, nextExpr, nextOps, result)) {
                     f=1;
                }
                nextNums.pop_back();
                nextExpr.pop_back();
                nextOps.pop_back();
            }
        }
    }

    return f;
}
bool solve(vector<int>& nums, vector<string>& expr, vector<string>& ops, string& result) {
    bool f = 0;
	if (nums.size() == 1) {
        if (fabs(nums[0] - TARGET) < EPSILON) {
            result = expr[0]; // 找到解,保存最终表达式
            if(!mp[result])cout << result << " = 24" << endl;
            mp[result]++;
            return true;
        }
        return false;
    }

    for (size_t i = 0; i < nums.size(); ++i) {
        for (size_t j = 0; j < nums.size(); ++j) {
            if (i == j) continue;

            vector<int> nextNums;
            vector<string> nextExpr;
            vector<string> nextOps;
            for (size_t k = 0; k < nums.size(); ++k) {
                if (k != i && k != j) {
                    nextNums.push_back(nums[k]);
                    nextExpr.push_back(expr[k]);
                    nextOps.push_back(ops[k]);
                }
            }

            // Try addition
            string newExpr="";
            if(i<j){
	            newExpr="";
	            if(needParentheses(ops[i], "+") ) {
	            	newExpr+=("("+expr[i]+")");
				}
				else{
					newExpr+=expr[i];
				}
	            newExpr += "+";
	           	if(needParentheses(ops[j], "+") ) {
	           		newExpr+=("("+expr[j]+")");
				}
				else{
					newExpr+=expr[j];
				}
				nextNums.push_back(nums[i] + nums[j]);
	            nextExpr.push_back(newExpr);
	            nextOps.push_back("+");
	            if (solve(nextNums, nextExpr, nextOps, result)) {
	                f=1;
	            }
	            nextNums.pop_back();
	            nextExpr.pop_back();
	            nextOps.pop_back();
			}
            // Try subtraction
            if(nums[i]>=nums[j]){
	            newExpr="";
	            if(needParentheses(ops[i], "-") ) {
	            	newExpr+=("("+expr[i]+")");
				}
				else{
					newExpr+=expr[i];
				}
	            newExpr += "-";;
	           	if(needParentheses(ops[j], "-") || needParentheses2(ops[j], "-")) {
	           		newExpr+=("("+expr[j]+")");
				}
				else{
					newExpr+=expr[j];
				}
	            nextNums.push_back(nums[i] - nums[j]);
	            nextExpr.push_back(newExpr);
	            nextOps.push_back("-");
	            if (solve(nextNums, nextExpr, nextOps, result)) {
	                f=1;
	            }
	            nextNums.pop_back();
	            nextExpr.pop_back();
	            nextOps.pop_back();
			}
			
            // Try multiplication
            if(i<j){
	            newExpr="";
	            if(needParentheses(ops[i], "*") ) {
	            	newExpr+=("("+expr[i]+")");
				}
				else{
					newExpr+=expr[i];
				}
	            newExpr += "*";;
	           	if(needParentheses(ops[j], "*") ) {
	           		newExpr+=("("+expr[j]+")");
				}
				else{
					newExpr+=expr[j];
				}
	            nextNums.push_back(nums[i] * nums[j]);
	            nextExpr.push_back(newExpr);
	            nextOps.push_back("*");
	            if (solve(nextNums, nextExpr, nextOps, result)) {
	                f=1;
	            }
	            nextNums.pop_back();
	            nextExpr.pop_back();
	            nextOps.pop_back();
	        }

            // Try division
            if (nums[j] != 0 && nums[i] % nums[j] == 0) {
            	newExpr="";
            	if(needParentheses(ops[i], "/") ) {
            		newExpr+=("("+expr[i]+")");
				}
				else{
					newExpr+=expr[i];
				}
                newExpr += "/";;
            	if(needParentheses(ops[j], "/") || needParentheses2(ops[j], "/")) {
            		newExpr+=("("+expr[j]+")");
				}
				else{
					newExpr+=expr[j];
				}
                nextNums.push_back(nums[i] / nums[j]);
                nextExpr.push_back(newExpr);
                nextOps.push_back("/");
                if (solve(nextNums, nextExpr, nextOps, result)) {
                    f=1;
                }
                nextNums.pop_back();
                nextExpr.pop_back();
                nextOps.pop_back();
            }
        }
    }

    return f;
}

string to_string(int i) {
    if (i == 0) return "0"; // 处理 0 的情况
    string s;
    while (i) {
        s = char(i % 10 + 48) + s;
        i /= 10;
    }
    return s;
}
int cnt=0;
int main() {
	while(1){
    vector<int> nums(4);
    vector<string> expr(4);
    vector<string> ops(4, ""); // 初始运算符为空
	
    for (int i = 0; i < 4; ++i) {
        cin >> nums[i];
        expr[i] = to_string(nums[i]); // 初始化表达式
    }
	
    string result;
    if (!solve(nums, expr, ops, result)) {
        cout << "无解" << endl;
    }}
//	
//	freopen("24dian.txt","w",stdout);
//	for(int i = 1; i <= 9; i++){
//		for(int j = i; j <= 9; j++){
//			for(int k = j; k <= 9; k++){
//				for(int l = k; l <= 9; l++){
//					vector<int> nums;
//				    vector<string> expr;
//				    nums.push_back(i);expr.push_back(to_string(i));
//				    nums.push_back(j);expr.push_back(to_string(j));
//				    nums.push_back(k);expr.push_back(to_string(k));
//				    nums.push_back(l);expr.push_back(to_string(l));
//				    vector<string> ops(4, "");
//				    string result="";
//					if(solve2(nums, expr, ops, result))cout << endl << i << ' ' << j << ' ' << k << ' ' << l << endl;
//				    if(solve(nums, expr, ops, result))cnt++;
//				}
//			}
//		}
//	} 
//	cout << "\n\n\n" << cnt;
    return 0;
}

main中上面部分输入4个数字,系统给解法。下面部分找出所有1~9的24点解法,输入文件。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值