2 条题解

  • 1
    @ 2026-9-21 18:53:08

    这里分享一种“简便的手动枚举方法” 不难发现,共有27种解决方案,那么不妨手动枚举27种情况,但是考虑到手写27种可能性的if语句有些麻烦,我们不妨先写一个简单的生成器,有:

    #include <iostream>
    char op(int t) {
        if (t == 0) return '+';
        if (t == 1) return '-';
        return '*';
    }
    int main() {
        bool first = true;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                for (int k = 0; k < 3; k++) {
                    char o1 = op(i), o2 = op(j), o3 = op(k);
                    // 27 种情况的判断与输出,第一条用 if,其余用 else if
                    std::cout << (first ? "if(" : "else if(");
                    std::cout << 'a' << o1 << 'b' << o2 << 'c' << o3 << "d==24) ";
                    std::cout << "std::cout<<";
                    std::cout << 'a' << "<<" << "\"" << o1 << "\"" << "<<";
                    std::cout << 'b' << "<<" << "\"" << o2 << "\"" << "<<";
                    std::cout << 'c' << "<<" << "\"" << o3 << "\"" << "<<";
                    std::cout << 'd' << "<<std::endl;" << std::endl;
    
                    first = false;
                }
            }
        }
        std::cout << "else std::cout<<-1<<std::endl;" << std::endl;
        return 0;
    }
    
    

    运行,得到:

    if(a+b+c+d==24) std::cout<<a<<"+"<<b<<"+"<<c<<"+"<<d<<std::endl;
    else if(a+b+c-d==24) std::cout<<a<<"+"<<b<<"+"<<c<<"-"<<d<<std::endl;
    else if(a+b+c*d==24) std::cout<<a<<"+"<<b<<"+"<<c<<"*"<<d<<std::endl;
    else if(a+b-c+d==24) std::cout<<a<<"+"<<b<<"-"<<c<<"+"<<d<<std::endl;
    else if(a+b-c-d==24) std::cout<<a<<"+"<<b<<"-"<<c<<"-"<<d<<std::endl;
    else if(a+b-c*d==24) std::cout<<a<<"+"<<b<<"-"<<c<<"*"<<d<<std::endl;
    else if(a+b*c+d==24) std::cout<<a<<"+"<<b<<"*"<<c<<"+"<<d<<std::endl;
    else if(a+b*c-d==24) std::cout<<a<<"+"<<b<<"*"<<c<<"-"<<d<<std::endl;
    else if(a+b*c*d==24) std::cout<<a<<"+"<<b<<"*"<<c<<"*"<<d<<std::endl;
    else if(a-b+c+d==24) std::cout<<a<<"-"<<b<<"+"<<c<<"+"<<d<<std::endl;
    else if(a-b+c-d==24) std::cout<<a<<"-"<<b<<"+"<<c<<"-"<<d<<std::endl;
    else if(a-b+c*d==24) std::cout<<a<<"-"<<b<<"+"<<c<<"*"<<d<<std::endl;
    else if(a-b-c+d==24) std::cout<<a<<"-"<<b<<"-"<<c<<"+"<<d<<std::endl;
    else if(a-b-c-d==24) std::cout<<a<<"-"<<b<<"-"<<c<<"-"<<d<<std::endl;
    else if(a-b-c*d==24) std::cout<<a<<"-"<<b<<"-"<<c<<"*"<<d<<std::endl;
    else if(a-b*c+d==24) std::cout<<a<<"-"<<b<<"*"<<c<<"+"<<d<<std::endl;
    else if(a-b*c-d==24) std::cout<<a<<"-"<<b<<"*"<<c<<"-"<<d<<std::endl;
    else if(a-b*c*d==24) std::cout<<a<<"-"<<b<<"*"<<c<<"*"<<d<<std::endl;
    else if(a*b+c+d==24) std::cout<<a<<"*"<<b<<"+"<<c<<"+"<<d<<std::endl;
    else if(a*b+c-d==24) std::cout<<a<<"*"<<b<<"+"<<c<<"-"<<d<<std::endl;
    else if(a*b+c*d==24) std::cout<<a<<"*"<<b<<"+"<<c<<"*"<<d<<std::endl;
    else if(a*b-c+d==24) std::cout<<a<<"*"<<b<<"-"<<c<<"+"<<d<<std::endl;
    else if(a*b-c-d==24) std::cout<<a<<"*"<<b<<"-"<<c<<"-"<<d<<std::endl;
    else if(a*b-c*d==24) std::cout<<a<<"*"<<b<<"-"<<c<<"*"<<d<<std::endl;
    else if(a*b*c+d==24) std::cout<<a<<"*"<<b<<"*"<<c<<"+"<<d<<std::endl;
    else if(a*b*c-d==24) std::cout<<a<<"*"<<b<<"*"<<c<<"-"<<d<<std::endl;
    else if(a*b*c*d==24) std::cout<<a<<"*"<<b<<"*"<<c<<"*"<<d<<std::endl;
    else std::cout<<-1<<std::endl;
    

    最终的代码:

    #include <iostream>
    int main(){
        int T;
        std::cin>>T;
        for(int i=0;i<T;i++){
            int a,b,c,d;
            std::cin>>a>>b>>c>>d;
            if(a+b+c+d==24) std::cout<<a<<"+"<<b<<"+"<<c<<"+"<<d<<std::endl;
    else if(a+b+c-d==24) std::cout<<a<<"+"<<b<<"+"<<c<<"-"<<d<<std::endl;
    else if(a+b+c*d==24) std::cout<<a<<"+"<<b<<"+"<<c<<"*"<<d<<std::endl;
    else if(a+b-c+d==24) std::cout<<a<<"+"<<b<<"-"<<c<<"+"<<d<<std::endl;
    else if(a+b-c-d==24) std::cout<<a<<"+"<<b<<"-"<<c<<"-"<<d<<std::endl;
    else if(a+b-c*d==24) std::cout<<a<<"+"<<b<<"-"<<c<<"*"<<d<<std::endl;
    else if(a+b*c+d==24) std::cout<<a<<"+"<<b<<"*"<<c<<"+"<<d<<std::endl;
    else if(a+b*c-d==24) std::cout<<a<<"+"<<b<<"*"<<c<<"-"<<d<<std::endl;
    else if(a+b*c*d==24) std::cout<<a<<"+"<<b<<"*"<<c<<"*"<<d<<std::endl;
    else if(a-b+c+d==24) std::cout<<a<<"-"<<b<<"+"<<c<<"+"<<d<<std::endl;
    else if(a-b+c-d==24) std::cout<<a<<"-"<<b<<"+"<<c<<"-"<<d<<std::endl;
    else if(a-b+c*d==24) std::cout<<a<<"-"<<b<<"+"<<c<<"*"<<d<<std::endl;
    else if(a-b-c+d==24) std::cout<<a<<"-"<<b<<"-"<<c<<"+"<<d<<std::endl;
    else if(a-b-c-d==24) std::cout<<a<<"-"<<b<<"-"<<c<<"-"<<d<<std::endl;
    else if(a-b-c*d==24) std::cout<<a<<"-"<<b<<"-"<<c<<"*"<<d<<std::endl;
    else if(a-b*c+d==24) std::cout<<a<<"-"<<b<<"*"<<c<<"+"<<d<<std::endl;
    else if(a-b*c-d==24) std::cout<<a<<"-"<<b<<"*"<<c<<"-"<<d<<std::endl;
    else if(a-b*c*d==24) std::cout<<a<<"-"<<b<<"*"<<c<<"*"<<d<<std::endl;
    else if(a*b+c+d==24) std::cout<<a<<"*"<<b<<"+"<<c<<"+"<<d<<std::endl;
    else if(a*b+c-d==24) std::cout<<a<<"*"<<b<<"+"<<c<<"-"<<d<<std::endl;
    else if(a*b+c*d==24) std::cout<<a<<"*"<<b<<"+"<<c<<"*"<<d<<std::endl;
    else if(a*b-c+d==24) std::cout<<a<<"*"<<b<<"-"<<c<<"+"<<d<<std::endl;
    else if(a*b-c-d==24) std::cout<<a<<"*"<<b<<"-"<<c<<"-"<<d<<std::endl;
    else if(a*b-c*d==24) std::cout<<a<<"*"<<b<<"-"<<c<<"*"<<d<<std::endl;
    else if(a*b*c+d==24) std::cout<<a<<"*"<<b<<"*"<<c<<"+"<<d<<std::endl;
    else if(a*b*c-d==24) std::cout<<a<<"*"<<b<<"*"<<c<<"-"<<d<<std::endl;
    else if(a*b*c*d==24) std::cout<<a<<"*"<<b<<"*"<<c<<"*"<<d<<std::endl;
    else std::cout<<-1<<std::endl;
        }
        return 0;
    }
    
    • 0
      @ 2025-9-22 21:27:28

      由于需要考虑运算符优先级,我们需要使用栈来模拟表达式的计算过程:

      处理乘法优先:遇到乘法时立即计算

      延迟加减法:遇到加减法时先压入栈,最后统一计算

      对于表达式 $a \enspace op1 \enspace b \enspace op2 \enspace c \enspace op3 \enspace d$:

      1. 初始化栈,将第一个数字压入栈

      2. 依次处理每个运算符和数字:

      • 如果是乘法:弹出栈顶元素,与当前数字相乘,结果压回栈

      • 如果是加法:将当前数字压入栈(正数)

      • 如果是减法:将当前数字的负数压入栈

      3.最后将栈中所有元素相加得到最终结果

      #include <bits/stdc++.h>
      using namespace std;
      const vector<char> OPS = {'+', '-', '*'};
      void solve()
      {
          vector<int> nums(4);
          for (auto &v : nums)
              cin >> v;
          for (auto op1 : OPS)
              for (auto op2 : OPS)
                  for (auto op3 : OPS)
                  {
                      vector<char> ops = {op1, op2, op3};
                      vector<int> stk;
                      stk.push_back(nums.front());
                      for (int i = 0; i < 3; i++)
                      {
                          if (ops[i] == '*')
                          {
                              int u = stk.back();
                              stk.pop_back();
                              stk.push_back(u * nums[i + 1]);
                          }
                          else if (ops[i] == '+')
                              stk.push_back(nums[i + 1]);
                          else
                              stk.push_back(-nums[i + 1]);
                      }
                      int ans = 0;
                      for (auto it : stk)
                          ans += it;
                      if (ans == 24)
                      {
                          cout << nums[0] << op1 << nums[1] << op2 << nums[2] << op3 << nums[3] << endl;
                          return;
                      }
                  }
          cout << -1 << endl;
      }
      
      int main()
      {
          ios::sync_with_stdio(false);
          cin.tie(0), cout.tie(0);
          int t;
          cin >> t;
          while (t--)
              solve();
          return 0;
      }
      
      • 1

      信息

      ID
      2726
      时间
      1000ms
      内存
      256MiB
      难度
      8
      标签
      (无)
      递交数
      21
      已通过
      6
      上传者