#include <bits/stdc++.h> using namespace std;

char f[4] = {'+', '-', '*', '/'}; // 运算符 int k[4], a, b, c, d, x, y, z; string s1, s2, s3, ans; bool flag;

int count(int x, int y, int ch) // 计算 { switch (ch) { case 1: return x + y; case 2: return x - y; case 3: return x * y; case 4: return x / y; } }

int main() { for (int i = 0; i < 4; i++) { cin >> k[i]; } ans = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; // ~ 是126,DEL 是127 for (a = 0; a < 4; a++) { for (b = 0; b < 4; b++) { if (a != b) { for (c = 0; c < 4; c++) { if (c != a && c != b) { for (d = 0; d < 4; d++) // 枚举四个数字 { if (d != a && d != b && d != c) { for (x = 0; x < 4; x++) { for (y = 0; y < 4; y++) { for (z = 0; z < 4; z++) { // 枚举三个运算符 flag = true; if (x != 3 || (k[a] % k[b] == 0)) { int p = count(k[a], k[b], x); if (flag) { if (y != 3 || (p % k[c] == 0)) { p = count(p, k[c], y); if (flag) { if (z != 3 || (p % k[d] == 0)) { p = count(p, k[d], z); if (flag) { if (p == 24) { s1 = '(' + to_string(k[a]) + f[x] + to_string(k[b]) + ')'; s1 = '(' + s1 + f[y] + to_string(k[c]) + ')'; s1 = '(' + s1 + f[z] + to_string(k[d]) + ')';

                                                                    if (s1 < ans || ans == "z")
                                                                    {
                                                                        ans = s1;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            flag = false;
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    flag = false;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            flag = false; // 显然,如果中间值出现小数,这个模拟就是不成立的
                                        }
                                        flag = true;
                                        if (x != 3 || (k[a] % k[b] == 0))
                                        {
                                            int p = count(k[a], k[b], x);
                                            if (flag)
                                            {
                                                if (z != 3 || (k[c] % k[d] == 0))
                                                {
                                                    int q = count(k[c], k[d], z);
                                                    if (flag)
                                                    {
                                                        if (y != 3 || (q != 0 && p % q == 0))
                                                        {
                                                            p = count(p, q, y);
                                                            if (flag)
                                                            {
                                                                if (p == 24)
                                                                {
                                                                    s1 = '(' + to_string(k[a]) + f[x] + to_string(k[b]) + ')';
                                                                    s2 = '(' + to_string(k[c]) + f[z] + to_string(k[d]) + ')';
                                                                    s1 = '(' + s1 + f[y] + s2 + ')';

                                                                    if (s1 < ans || ans == "z")
                                                                    {
                                                                        ans = s1;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            flag = false;
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    flag = false;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            flag = false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
cout << ans << endl;
return 0;
去除多余的大括号#include bitsstdc++husing namespace std;char f4 = + - ; 运算符int k4 a b c d x y z;string s1 s2 s3 ans;bool flag;int countint x int y int ch 计算 switch ch case 1 return x + y;

原文地址: https://www.cveoy.top/t/topic/iCMp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录