免费 二十四点计算程序 准赢!!!!!!!1

#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
class Calculator {
	public:
		double evaluatePostfix(const std::string& expression);
	private:
		bool isOperator(char c);
		int precedence(char op);
		double applyOperation(double operand1, double operand2, char operation);
};
double Calculator::evaluatePostfix(const std::string& expression) {
	std::istringstream iss(expression);
	std::stack<double> stack;
	std::string token;
	while (iss >> token) {
		if (!isOperator(token[0]) && token != " ") {
			stack.push(std::stod(token));
		} else {
			double secondOperand = stack.top();
			stack.pop();
			double firstOperand = stack.top();
			stack.pop();
			double result = applyOperation(firstOperand, secondOperand, token[0]);
			stack.push(result);
		}
	}
	return stack.top();
}

bool Calculator::isOperator(char c) {
	switch(c) {
		case '+':
		case '-':
		case '*':
		case '/':return true;	
		default:return false;	
	}
}
int Calculator::precedence(char op) {
	if(op == '+' || op == '-') return 1;
	if(op == '*' || op == '/') return 2;
	return 0;
}
double Calculator::applyOperation(double operand1, double operand2, char operation) {
	switch(operation) {
		case '+':return operand1 + operand2;	
		case '-':return operand1 - operand2;
		case '*':return operand1 * operand2;
		case '/':return operand1 / operand2;	
	}
}
class RPNToInfix {
	private:
		stack<vector<string>> stack;
	public:
		string convert(const string& rpn) {
			istringstream iss(rpn);
			string token;
			while (iss >> token) {
				if (isOperator(token)) {
					if (stack.size() >= 2) {
						vector<string> operand2 = stack.top();
						stack.pop();
						vector<string> operand1 = stack.top();
						stack.pop();
						vector<string> newExpr;
						newExpr.push_back("(");
						newExpr.insert(newExpr.end(), operand1.begin(), operand1.end());
						newExpr.push_back(" " + token + " ");
						newExpr.insert(newExpr.end(), operand2.begin(), operand2.end());
						newExpr.push_back(")");
						stack.push(newExpr);
					}
				} else {
					vector<string> operand;
					operand.push_back(token);
					stack.push(operand);
				}
			}
			vector<string> result = stack.top();
			string infixExpr;
			for (const string& part : result) {
				infixExpr += part;
			}
			return infixExpr;
		}
	private:
		bool isOperator(const string& token) {
			return token == "+" || token == "-" || token == "*" || token == "/" || token == "^";
		}
};
Calculator cal;
RPNToInfix rpn;
string q1,q2,q3,q4;
string re[10000000];
long long re_len;
void dfs(long long l,string in) {
	if(l>3) {
		if(cal.evaluatePostfix(in)==24) {
			string out=rpn.convert(in);
			for(int i=1; i<=re_len; i++)if(re[i]==out)return;
			re[++re_len]=out;
			cout<<out<<endl;
		}
		return;
	}
	string cmp=in;
	for(int i=0; i<cmp.length(); i++) {
		while(cmp[i]!=' '&&i<cmp.length())i++;
		while(cmp[i]==' '&&i<cmp.length())i++;
		while(cmp[i]!=' '&&i<cmp.length())i++;
		i++;
		if(cmp[i]==' ') {
			cmp[i]='+';
			dfs(l+1,cmp);
			cmp[i]='-';
			dfs(l+1,cmp);
			cmp[i]='*';
			dfs(l+1,cmp);
			cmp[i]='/';
			dfs(l+1,cmp);
			cmp[i]=' ';
		}
	}
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	while(1) {
		cin>>q1>>q2>>q3>>q4;
		dfs(1,q1+"   "+q2+"   "+q3+"   "+q4+"      ");
		dfs(1,q1+"   "+q2+"   "+q4+"   "+q3+"      ");
		dfs(1,q1+"   "+q3+"   "+q2+"   "+q4+"      ");
		dfs(1,q1+"   "+q3+"   "+q4+"   "+q2+"      ");
		dfs(1,q1+"   "+q4+"   "+q2+"   "+q3+"      ");
		dfs(1,q1+"   "+q4+"   "+q3+"   "+q2+"      ");
		dfs(1,q2+"   "+q1+"   "+q3+"   "+q4+"      ");
		dfs(1,q2+"   "+q1+"   "+q4+"   "+q3+"      ");
		dfs(1,q2+"   "+q3+"   "+q1+"   "+q4+"      ");
		dfs(1,q2+"   "+q3+"   "+q4+"   "+q1+"      ");
		dfs(1,q2+"   "+q4+"   "+q1+"   "+q3+"      ");
		dfs(1,q2+"   "+q4+"   "+q3+"   "+q1+"      ");
		dfs(1,q3+"   "+q1+"   "+q2+"   "+q4+"      ");
		dfs(1,q3+"   "+q1+"   "+q4+"   "+q2+"      ");
		dfs(1,q3+"   "+q2+"   "+q1+"   "+q4+"      ");
		dfs(1,q3+"   "+q2+"   "+q4+"   "+q1+"      ");
		dfs(1,q3+"   "+q4+"   "+q1+"   "+q2+"      ");
		dfs(1,q3+"   "+q4+"   "+q2+"   "+q1+"      ");
		dfs(1,q4+"   "+q1+"   "+q2+"   "+q3+"      ");
		dfs(1,q4+"   "+q1+"   "+q3+"   "+q2+"      ");
		dfs(1,q4+"   "+q2+"   "+q1+"   "+q3+"      ");
		dfs(1,q4+"   "+q2+"   "+q3+"   "+q1+"      ");
		dfs(1,q4+"   "+q3+"   "+q1+"   "+q2+"      ");
		dfs(1,q4+"   "+q3+"   "+q2+"   "+q1+"      ");
		system("pause");
	}
	return 0;
}

二十四点作弊程序,和别人玩二十四点绝对赢!

input:

1
2
3
4

output:

(((1 + 2) + 3) * 4)
(((1 * 2) * 3) * 4)
(((1 * 2) * 4) * 3)
(((1 + 3) + 2) * 4)
(((1 * 3) * 2) * 4)
(((1 * 3) * 4) * 2)
(((1 * 4) * 2) * 3)
(((1 * 4) * 3) * 2)
(((2 + 1) + 3) * 4)
(((2 * 1) * 3) * 4)
(((2 / 1) * 3) * 4)
(((2 * 1) * 4) * 3)
(((2 / 1) * 4) * 3)
(((2 + 3) + 1) * 4)
(((2 * 3) * 1) * 4)
(((2 * 3) / 1) * 4)
(((2 * 3) * 4) * 1)
(((2 * 3) * 4) / 1)
(((2 * 4) * 1) * 3)
(((2 * 4) / 1) * 3)
(((2 * 4) * 3) * 1)
(((2 * 4) * 3) / 1)
(((3 + 1) + 2) * 4)
(((3 * 1) * 2) * 4)
(((3 / 1) * 2) * 4)
(((3 * 1) * 4) * 2)
(((3 / 1) * 4) * 2)
(((3 + 2) + 1) * 4)
(((3 * 2) * 1) * 4)
(((3 * 2) / 1) * 4)
(((3 * 2) * 4) * 1)
(((3 * 2) * 4) / 1)
(((3 * 4) * 1) * 2)
(((3 * 4) / 1) * 2)
(((3 * 4) * 2) * 1)
(((3 * 4) * 2) / 1)
(((4 * 1) * 2) * 3)
(((4 / 1) * 2) * 3)
(((4 * 1) * 3) * 2)
(((4 / 1) * 3) * 2)
(((4 * 2) * 1) * 3)
(((4 * 2) / 1) * 3)
(((4 * 2) * 3) * 1)
(((4 * 2) * 3) / 1)
(((4 * 3) * 1) * 2)
(((4 * 3) / 1) * 2)
(((4 * 3) * 2) * 1)
(((4 * 3) * 2) / 1)

点个赞吧!!QwQ!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值