【支持库】不定个数数字数组的24点计算

小肥羊 1月前 224 v 1.0 2024-08-11

今天陪孩子做24点,于是简单写了个24点计算方法,分享探讨下:

import console; 
import yang.dian24;
nums = {1,3,4,15,4,5};
..console.dump("初始值",nums)

t1 = ..time.tick();
res,e,s = yang.dian24.compute24(nums); 
..console.dump("取一个结果",res,e,s )
..console.dump("耗时",..time.tick()-t1)

..console.log()


..console.askYesNo("按Y键继续,按N键取消")

console.pause(true);


上传的附件:
最新回复 (5)
  • 小光芒 1月前
    0 2
    带娃还顺手写个算法
  • 光庆 1月前
    0 3

    带娃顺手写个软件卖钱 

  • mndsoft 1月前
    0 4

    claude-3.5-sonnet 写的,不懂,看看输出对不对

    import win.ui;
    import math;
    /*DSG{{*/
    var winform = win.form(text="24点计算器";right=399;bottom=701)
    winform.add(
    button={cls="button";text="计算";left=340;top=20;right=380;bottom=50;z=5};
    edit1={cls="edit";left=20;top=20;right=80;bottom=50;edge=1;z=1};
    edit2={cls="edit";left=100;top=20;right=160;bottom=50;edge=1;z=2};
    edit3={cls="edit";left=180;top=20;right=240;bottom=50;edge=1;z=3};
    edit4={cls="edit";left=260;top=20;right=320;bottom=50;edge=1;z=4};
    edit5={cls="edit";left=20;top=70;right=380;bottom=684;edge=1;multiline=1;vscroll=1;z=6}
    )
    /*}}*/
    
    // 定义运算符
    operators = {"+", "-", "*", "/"};
    
    // 执行运算
    calculate = function(a, b, op) {
        if(op == "+") return a + b;
        if(op == "-") return a - b;
        if(op == "*") return a * b;
        if(op == "/") return b != 0 ? a / b : null;
    }
    
    // 生成所有可能的排列
    permute = function(arr, start = 1) {
        if (start == #arr) {
            return {arr};
        }
        var result = {};
        for(i=start;#arr;1) {
            arr[start], arr[i] = arr[i], arr[start];
            var subPermutations = permute(arr, start + 1);
            for(j=1;#subPermutations;1) {
                table.push(result, table.clone(subPermutations[j]));
            }
            arr[start], arr[i] = arr[i], arr[start];
        }
        return result;
    }
    
    // 解决24点问题
    solve24 = function(nums) {
        var solutions = {};
        var permutations = permute(nums);
        
        for(p=1;#permutations;1) {
            var nums = permutations[p];
            for(i=1;4;1) {
                for(j=1;4;1) {
                    for(k=1;4;1) {
                        var a = calculate(nums[1], nums[2], operators[i]);
                        if (a === null) continue;
                        var b = calculate(a, nums[3], operators[j]);
                        if (b === null) continue;
                        var c = calculate(b, nums[4], operators[k]);
                        if (c === null) continue;
                        if (math.abs(c - 24) < 0.000001) {
                            var expr = string.format("((%g%s%g)%s%g)%s%g", 
                                nums[1], operators[i], nums[2], operators[j], nums[3], operators[k], nums[4]);
                            table.push(solutions, expr + " = 24");
                        }
                        
                        a = calculate(nums[3], nums[4], operators[k]);
                        if (a === null) continue;
                        b = calculate(nums[2], a, operators[j]);
                        if (b === null) continue;
                        c = calculate(nums[1], b, operators[i]);
                        if (c === null) continue;
                        if (math.abs(c - 24) < 0.000001) {
                            var expr = string.format("%g%s(%g%s(%g%s%g))", 
                                nums[1], operators[i], nums[2], operators[j], nums[3], operators[k], nums[4]);
                            table.push(solutions, expr + " = 24");
                        }
                    }
                }
            }
        }
        return solutions;
    }
    
    // 按钮点击事件
    winform.button.oncommand = function(id,event){
        var nums = {
            tonumber(winform.edit1.text),
            tonumber(winform.edit2.text),
            tonumber(winform.edit3.text),
            tonumber(winform.edit4.text)
        };
        
        // 检查输入是否有效
        for(i=1;4;1) {
            if(type(nums[i]) != "number") {
                winform.edit5.text = "请在所有输入框中输入有效的数字。";
                return;
            }
        }
        
        winform.edit5.text = "正在计算,请稍候...";
        win.delay(100);
        
        var solutions = solve24(nums);
        
        if(#solutions > 0) {
            winform.edit5.text = string.join(solutions, '\r\n');
        } else {
            winform.edit5.text = "没有找到解法。";
        }
    }
    
    winform.show();
    win.loopMessage();


  • 近我者赤 1月前
    0 5

    好多是错的,下面出现了未给出的数

  • 小肥羊 1月前
    0 6
    直接封装了一个库,用起来方便点,有需要的自取
返回