#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
int cal(int a, int b, const string &op) {
int res = 0;
if (op == "+") {
res = a + b;
} else if (op == "-") {
res = a - b;
} else if (op == "*") {
res = a * b;
} else if (op == "**") {
res = pow(a, b);
} else if (op == "%") {
res = a % b;
} else if (op == "//") {
res = a / b;
} else if (op == "|") {
res = a | b;
} else if (op == "&") {
res = a & b;
} else if (op == "^") {
res = a ^ b;
} else if (op == "<<") {
res = a << b;
} else if (op == ">>") {
res = a >> b;
} else {
cout << "没有这个运算符:" << op << "!" << endl;
exit(-1);
}
return res;
}
vector<int> path_nums;
vector<string> path_op;
vector<vector<int>> res_nums;
vector<vector<string>> res_op;
void select_num(vector<int> &nums, int visited) {
if (path_nums.size() == 4) {
res_nums.push_back(path_nums);
return;
}
for (int i = 0; i < nums.size(); i++) {
if (visited >> i & 1) {
continue;
}
path_nums.push_back(nums[i]);
select_num(nums, visited | (1 << i));
path_nums.pop_back();
}
}
void select_op(vector<string> &ops, int visited) {
if (path_op.size() == 3) {
res_op.push_back(path_op);
return;
}
for (int i = 0; i < ops.size(); i++) {
if (visited >> i & 1) {
continue;
}
path_op.push_back(ops[i]);
select_op(ops, visited | (1 << i));
path_op.pop_back();
}
}
bool handler(vector<int> &nums, vector<string> &ops) {
path_nums.clear(), path_op.clear();
res_nums.clear(), res_op.clear();
select_num(nums, 0);
select_op(ops, 0);
int cnt = 0;
for (auto n : res_nums) {
// 只打印 3 条组合方式
int printcnt = 3;
for (auto op: res_op) {
int a = cal(n[0], n[1], op[0]);
int b = cal(a, n[2], op[1]);
int c = cal(b, n[3], op[2]);
if (c == 1024) {
if (printcnt == 0) {
break;
}
cnt++;
printcnt--;
printf("\t %d %s %d %s %d %s %d = %d\n", n[0], op[0].c_str(), n[1], op[1].c_str(), n[2], op[2].c_str(),
n[3], c);
}
}
}
if (cnt == 0) {
return false;
}
return true;
}
void add_num(vector<int> &nums, vector<string> &ops) {
bool check = false;
for (int i = 0; i < 100; i++) {
nums.push_back(i);
cout << i << " :" << endl;
bool b = handler(nums, ops);
if (b && !check) check = true;
nums.pop_back();
}
nums.push_back(1024);
cout << 1024 << " :";
bool b = handler(nums, ops);
if (b && !check) check = true;
nums.pop_back();
if (!check) {
cout << "你要多收集两张数字牌才能组成 1024!" << endl;
}
}
void add_op(vector<int> &nums, vector<string> &ops) {
vector<string> allops = {"+", "-", "*", "**", "%", "//", "|", "&", "^", "<<", ">>"};
bool check = false;
for (int i = 0; i < allops.size(); i++) {
ops.push_back(allops[i]);
cout << allops[i] << " : " << endl;
bool b = handler(nums, ops);
if (b && !check) check = true;
ops.pop_back();
}
if (!check) {
cout << "你要多收集两张运算符牌才能组成 1024!" << endl;
}
}
void need(vector<int> &nums, vector<string> &ops) {
cout << "你需要的数字牌:" << endl;
add_num(nums, ops);
cout << "你需要的运算符牌:" << endl;
add_op(nums, ops);
}
int main() {
//vector<int> nums = {32, 9, 31, 9, 2, 30, 32, 2, 0, 2, 2};
//vector<string> ops = {">>", "*", "&", "|"};
// vector<int> nums = {0, 1, 6, 8};
// vector<string> ops = {"|", "<<", "<<"};
vector<int> nums = {21, 2, 17, 34, 11, 1, 26, 2, 17, 2, 1, 23};
vector<string> ops = {">>", ">>", "*", "+", "|", "|"};
if (nums.size() < 4) {
cout << "数字牌不足 4 张!" << endl;
need(nums, ops);
return 0;
}
if (ops.size() < 3) {
cout << "运算符牌不足 3 张!" << endl;
need(nums, ops);
return 0;
}
if (nums.size() > 31) {
cout << "数字牌太多啦!" << endl;
return 0;
}
if (ops.size() > 31) {
cout << "运算符牌太多啦!" << endl;
return 0;
}
if (!handler(nums, ops)) {
cout << "你的牌不能组成 1024" << endl;
need(nums, ops);
return 0;
}
return 0;
}
10-17
7651