题解 | #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;
}

全部评论
大佬,牛批
点赞 回复 分享
发布于 04-13 18:44 江西

相关推荐

03-27 17:33
门头沟学院 Java
代码飞升:同学院本,你要注意hr当天有没有回复过,早上投,还要打招呼要推销自己,不要一个劲投
点赞 评论 收藏
分享
评论
6
2
分享

创作者周榜

更多
牛客网
牛客企业服务