How to design an algorithm to calculate countdown style maths number puzzle

后端 未结 10 1139
清酒与你
清酒与你 2020-11-30 00:35

I have always wanted to do this but every time I start thinking about the problem it blows my mind because of its exponential nature.

The problem solver I want to be

10条回答
  •  春和景丽
    2020-11-30 01:28

    Input is obviously a set of digits and operators: D={1,3,3,6,7,8,3} and Op={+,-,*,/}. The most straight forward algorithm would be a brute force solver, which enumerates all possible combinations of these sets. Where the elements of set Op can be used as often as wanted, but elements from set D are used exactly once. Pseudo code:

    D={1,3,3,6,7,8,3}
    Op={+,-,*,/}
    Solution=348
    for each permutation D_ of D:
       for each binary tree T with D_ as its leafs:
           for each sequence of operators Op_ from Op with length |D_|-1:
               label each inner tree node with operators from Op_
               result = compute T using infix traversal
               if result==Solution
                  return T
    return nil
    

    Other than that: read jedrus07's and HPM's answers.

提交回复
热议问题