题解 | #24点游戏算法#

24点游戏算法

http://www.nowcoder.com/practice/fbc417f314f745b1978fc751a54ac8cb

考虑括号的解法

看了其他题解全都是没有考虑括号的,虽然有的也能对(但这只是因为,审核数据不够刁钻),所以把带括号的解法分享出来,欢迎批评指正,相互交流。
#include<iostream>
#include<algorithm>
using namespace std;

char ops[4] = {'+', '-', '*', '/'};

double cal(double db1, double db2, char op) {
    double ret;
    switch (op) {
        case '+':
            ret = db1 + db2;
            break;
        case '-':
            ret = db1 - db2;
            break;
        case '*':
            ret = db1 * db2;
            break;
        case '/':
            ret = db1 / db2;
            break;
    };
    return ret;
}

int main() {
    int num[4];
    cin >> num[0] >> num[1] >> num[2] >> num[3];
    sort(num, num + 4);
    do {
        //无括号情况
        for (int i = 0; i < 4; i++) {
            double res;
            if (i == 3 && num[1] == 0) {
                continue;
            } else {
                res = cal(num[0], num[1], ops[i]);
            }
            for (int j = 0; j < 4; j++) {
                if (j == 3 && num[2] == 0) {
                    continue;
                } else {
                    res = cal(res, num[2], ops[j]);
                }
                for (int k = 0; k < 4; k++) {
                    if (k == 3 && num[3] == 0) {
                        continue;
                    } else {
                        res = cal(res, num[3], ops[k]);
                    }
                    if (res == 24) {
                        cout << "true" << endl;
                        return 0;
                    }
                }
            }
        }
        //num1,num2有括号
        for (int i = 0; i < 4; i++) {
            double val12;
            if (i == 3 && num[2] == 0) {
                continue;
            } else {
                val12 = cal(num[1], num[2], ops[i]);
            }
            // 计算完num1,num2后从左往右
            for (int j = 0; j < 4; j++) {
                double res;
                if (j == 3 && val12 == 0) {
                    continue;
                } else {
                    res = cal(num[0], val12, ops[j]);
                }
                for (int k = 0; k < 4; k++) {
                    if (k == 3 && num[3] == 0) {
                        continue;
                    } else {
                        res = cal(res, num[3], ops[k]);
                    }
                    if (res == 24) {
                        cout << "true" << endl;
                        return 0;
                    }
                }
            }
            // 计算完num1,num2,在计算val12和num3后从左往右
            for (int j = 0; j < 4; j++) {
                double res;
                if (j == 3 && num[3] == 0) {
                    continue;
                } else {
                    res = cal(val12, num[3], ops[j]);
                }
                for (int k = 0; k < 4; k++) {
                    if (k == 3 && res == 0) {
                        continue;
                    } else {
                        res = cal(num[0], res, ops[k]);
                    }
                    if (res == 24) {
                        cout << "true" << endl;
                        return 0;
                    }
                }
            }
        }
        //先计算num2,num3;
        for (int i = 0; i < 4; i++) {
            double val23;
            if (i == 3 && num[3] == 0) {
                continue;
            } else {
                val23 = cal(num[2], num[3], ops[i]);
            }
            //先计算num2,num3;再和num1计算,后从左往右
            for (int j = 0; j < 4; j++) {
                double res;
                if (j == 3 && val23 == 0) {
                    continue;
                } else {
                    res = cal(num[1], val23, ops[j]);
                }
                for (int k = 0; k < 4; k++) {
                    if (k == 3 && res == 0) {
                        continue;
                    } else {
                        res = cal(num[0], res,  ops[k]);
                    }
                    if (res == 24) {
                        cout << "true" << endl;
                        return 0;
                    }
                }
            }
            //先计算num2,num3,然后从左往右
            for (int j = 0; j < 4; j++) {
                double res;
                if (j == 3 && num[1] == 0) {
                    continue;
                } else {
                    res = cal(num[0], num[1], ops[j]);
                }
                for (int k = 0; k < 4; k++) {
                    if (k == 3 && val23 == 0) {
                        continue;
                    } else {
                        res = cal(res, val23, ops[k]);
                    }
                    if (res == 24) {
                        cout << "true" << endl;
                        return 0;
                    }
                }
            }
        }
    } while (next_permutation(num, num + 4));
    cout<<"false"<<endl;
    return 0;
}

全部评论
8 8 8 8这个用例过不了
点赞 回复 分享
发布于 2025-08-13 20:10 北京
大佬,牛批
点赞 回复 分享
发布于 2025-04-13 18:44 江西

相关推荐

首先讲三个故事,关于牛客的事件一:2024年,牛客上有一对高学历情侣,求职方向与我当时一致,都是嵌入式方向。他们恰好是我的朋友,专业能力和学历背景都很扎实,也因此拿到了不少优质offer。和很多求职者一样,他们把offer情况整理后发在平台上,本意是记录与交流,但很快引发了争议。有声音指责他们“集邮”“不释放名额”,认为这种展示本身就是一种炫耀。最终讨论失控,当事人删除内容,事件也很快被遗忘。事件二:小红书评论区,一条评价获得了不少共鸣:“感觉牛客就是当年那群做题区毕业了开始找工作还收不住那股味,颇有一种从年级第一掉到年纪第二后抱怨考不上大学的味道”,这条评论被水印里这个同学转发到牛客后,评论...
小型域名服务器:当看到别人比自己强的时候,即便这是对方应得的,很多人会也下意识的歪曲解构对方的意图,来消解自己在这本就不存在的比较中输掉的自信,从而平白制造出很多无谓的争论。比如你会在空余时间来写优质好文,而我回家只会暗区突围,那么我就可以作为键盘侠在这里评论你是不是XXXXXXXX。即便我自己都知道这是假的,但只要这没那么容易证伪,那么当你开始回应的时候,脏水就已经泼出去了,后面可能会有更多的人带着情绪来给我点赞,而毫不关注你写的文章内容本身是啥了。
SAGIMA牛马咖啡
点赞 评论 收藏
分享
评论
8
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务