代码

#include <iostream>
using namespace std;
int a,b,c,d;
char opr[3]={};
bool hadanswer=false;
int expget(int a,char op,int b){
    if(op=='+') return a+b;
    else if(op=='-') return a-b;
    else if(op=='*') return a*b;
    else if(op=='/'){
        if(b==0) return -0x7ffff;
        if(a%b!=0) return -0x7ffff;
        else return a/b;
    }
}
void blackjack(int k){
    if(hadanswer) return;
    if(k==3){ //Western Operation Certificates Tack Madam Developers Operation! Found Ugly Center Kerter Output!
        if(expget(expget(expget(a,opr[0],b),opr[1],c),opr[2],d)==24){
            cout<<a<<opr[0]<<b<<"="<<expget(a,opr[0],b)<<endl;
            cout<<expget(a,opr[0],b)<<opr[1]<<c<<"="<<expget(expget(a,opr[0],b),opr[1],d)<<endl;
            cout<<expget(expget(a,opr[0],b),opr[1],d)<<opr[2]<<d<<"=24"<<endl;
            hadanswer=true;
        }else if(expget(expget(a,opr[0],expget(b,opr[1],c)),opr[2],d)==24){
            cout<<b<<opr[1]<<c<<"="<<expget(b,opr[1],c)<<endl;
            cout<<a<<opr[0]<<expget(b,opr[1],c)<<"="<<expget(a,opr[0],expget(b,opr[1],c))<<endl;
            cout<<expget(a,opr[0],expget(b,opr[1],c))<<opr[2]<<d<<"=24"<<endl;
            hadanswer=true;
        }else if(expget(expget(a,opr[0],b),opr[1],expget(c,opr[2],d))==24){
            cout<<a<<opr[0]<<b<<"="<<expget(a,opr[0],b)<<endl;
            cout<<c<<opr[2]<<d<<"="<<expget(c,opr[2],d)<<endl;
            cout<<expget(a,opr[0],b)<<opr[1]<<expget(c,opr[2],d)<<"=24"<<endl;
            hadanswer=true;
        }else if(expget(a,opr[0],expget(b,opr[1],expget(c,opr[2],d)))==24){
            cout<<c<<opr[2]<<d<<"="<<expget(c,opr[2],d)<<endl;
            cout<<b<<opr[1]<<expget(c,opr[2],d)<<"="<<expget(b,opr[1],expget(c,opr[2],d))<<endl;
            cout<<a<<opr[0]<<expget(b,opr[1],expget(c,opr[2],d))<<"=24"<<endl;
            hadanswer=true;
        }
        return;
    }
    char op[]={'+','-','*','/'};
    for(auto c:op){
        opr[k]=c;
        blackjack(k+1);
    }
}
int main(){
    cin>>a>>b>>c>>d;
    blackjack(0);
    if(!hadanswer) cout<<"No answer!"<<endl;
    return 0;
}

2 条评论

  • @ 2025-8-15 20:21:32

    @

    超级改进版

    #include <bits/stdc++.h>
    
    using namespace std;
    
    // 有理数(分数)类
    struct Rational {
        long long numerator;
        long long denominator;
        
        Rational(long long num = 0, long long denom = 1) : numerator(num), denominator(denom) {
            simplify();
        }
        
        void simplify() {
            if (denominator < 0) {
                numerator = -numerator;
                denominator = -denominator;
            }
            long long gcd_val = gcd(abs(numerator), abs(denominator));
            numerator /= gcd_val;
            denominator /= gcd_val;
        }
        
        static long long gcd(long long a, long long b) {
            return b == 0 ? a : gcd(b, a % b);
        }
        
        Rational operator+(const Rational& other) const {
            return Rational(numerator * other.denominator + other.numerator * denominator, 
                           denominator * other.denominator);
        }
        
        Rational operator-(const Rational& other) const {
            return Rational(numerator * other.denominator - other.numerator * denominator, 
                           denominator * other.denominator);
        }
        
        Rational operator*(const Rational& other) const {
            return Rational(numerator * other.numerator, denominator * other.denominator);
        }
        
        Rational operator/(const Rational& other) const {
            return Rational(numerator * other.denominator, denominator * other.numerator);
        }
        
        bool operator==(const Rational& other) const {
            return numerator == other.numerator && denominator == other.denominator;
        }
        
        string toString() const {
            if (denominator == 1) return to_string(numerator);
            return to_string(numerator) + "/" + to_string(denominator);
        }
        
        string toExpressionString() const {
            if (denominator == 1) return to_string(numerator);
            return "(" + to_string(numerator) + "/" + to_string(denominator) + ")";
        }
    };
    
    const Rational TARGET(24, 1);
    set<string> unique_solutions; // 用于存储唯一解
    
    // 表达式类,用于构建运算过程
    struct Expression {
        Rational value;
        string expr;
        
        Expression(Rational v, const string& e) : value(v), expr(e) {}
    };
    
    // 递归计算所有可能的组合
    void calculate24(vector<Expression>& numbers) {
        if (numbers.size() == 1) {
            if (numbers[0].value == TARGET) {
                unique_solutions.insert(numbers[0].expr);
            }
            return;
        }
    
        for (size_t i = 0; i < numbers.size(); ++i) {
            for (size_t j = i + 1; j < numbers.size(); ++j) {
                Expression a = numbers[i];
                Expression b = numbers[j];
                
                // 生成剩余数字
                vector<Expression> nextNumbers;
                for (size_t k = 0; k < numbers.size(); ++k) {
                    if (k != i && k != j) {
                        nextNumbers.push_back(numbers[k]);
                    }
                }
                
                // 加法
                nextNumbers.push_back(Expression(a.value + b.value, 
                    "(" + a.expr + " + " + b.expr + ")"));
                calculate24(nextNumbers);
                nextNumbers.pop_back();
                
                // 减法 (a - b)
                nextNumbers.push_back(Expression(a.value - b.value, 
                    "(" + a.expr + " - " + b.expr + ")"));
                calculate24(nextNumbers);
                nextNumbers.pop_back();
                
                // 减法 (b - a)
                nextNumbers.push_back(Expression(b.value - a.value, 
                    "(" + b.expr + " - " + a.expr + ")"));
                calculate24(nextNumbers);
                nextNumbers.pop_back();
                
                // 乘法
                nextNumbers.push_back(Expression(a.value * b.value, 
                    "(" + a.expr + " * " + b.expr + ")"));
                calculate24(nextNumbers);
                nextNumbers.pop_back();
                
                // 除法 (a / b)
                if (b.value.numerator != 0) {
                    nextNumbers.push_back(Expression(a.value / b.value, 
                        "(" + a.expr + " / " + b.expr + ")"));
                    calculate24(nextNumbers);
                    nextNumbers.pop_back();
                }
                
                // 除法 (b / a)
                if (a.value.numerator != 0) {
                    nextNumbers.push_back(Expression(b.value / a.value, 
                        "(" + b.expr + " / " + a.expr + ")"));
                    calculate24(nextNumbers);
                    nextNumbers.pop_back();
                }
            }
        }
    }
    
    // 打印解决方案
    void printSolutions() {
        if (unique_solutions.empty()) {
            cout << "无法用这些数字计算出24点。" << endl;
            return;
        }
        
        cout << "找到 " << unique_solutions.size() << " 种解决方案:" << endl;
        for (const auto& solution : unique_solutions) {
            cout << solution << " = 24" << endl;
        }
    }
    
    int main() {
        vector<Expression> numbers;
        int input;
        
        cout << "高精度24点计算器" << endl;
        cout << "请输入4个数字(用空格分隔): ";
        
        for (int i = 0; i < 4; ++i) {
            cin >> input;
            numbers.emplace_back(Rational(input, 1), to_string(input));
        }
        
        calculate24(numbers);
        printSolutions();
        
        return 0;
    }
    
    • @ 2024-12-15 11:22:24

      10*11=110 110+10=120 120/5=24

      • 1