《力扣1024活动》卡牌一键计算

115 篇文章 3 订阅
#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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leimingzeOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值