Finding all possible combinations of numbers to reach a given sum

前端 未结 30 3109
一个人的身影
一个人的身影 2020-11-21 06:39

How would you go about testing all possible combinations of additions from a given set N of numbers so they add up to a given final number?

A brief exam

30条回答
  •  一整个雨季
    2020-11-21 07:21

    The solution of this problem has been given a million times on the Internet. The problem is called The coin changing problem. One can find solutions at http://rosettacode.org/wiki/Count_the_coins and mathematical model of it at http://jaqm.ro/issues/volume-5,issue-2/pdfs/patterson_harmel.pdf (or Google coin change problem).

    By the way, the Scala solution by Tsagadai, is interesting. This example produces either 1 or 0. As a side effect, it lists on the console all possible solutions. It displays the solution, but fails making it usable in any way.

    To be as useful as possible, the code should return a List[List[Int]]in order to allow getting the number of solution (length of the list of lists), the "best" solution (the shortest list), or all the possible solutions.

    Here is an example. It is very inefficient, but it is easy to understand.

    object Sum extends App {
    
      def sumCombinations(total: Int, numbers: List[Int]): List[List[Int]] = {
    
        def add(x: (Int, List[List[Int]]), y: (Int, List[List[Int]])): (Int, List[List[Int]]) = {
          (x._1 + y._1, x._2 ::: y._2)
        }
    
        def sumCombinations(resultAcc: List[List[Int]], sumAcc: List[Int], total: Int, numbers: List[Int]): (Int, List[List[Int]]) = {
          if (numbers.isEmpty || total < 0) {
            (0, resultAcc)
          } else if (total == 0) {
            (1, sumAcc :: resultAcc)
          } else {
            add(sumCombinations(resultAcc, sumAcc, total, numbers.tail), sumCombinations(resultAcc, numbers.head :: sumAcc, total - numbers.head, numbers))
          }
        }
    
        sumCombinations(Nil, Nil, total, numbers.sortWith(_ > _))._2
      }
    
      println(sumCombinations(15, List(1, 2, 5, 10)) mkString "\n")
    }
    

    When run, it displays:

    List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
    List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2)
    List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2)
    List(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2)
    List(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2)
    List(1, 1, 1, 1, 1, 2, 2, 2, 2, 2)
    List(1, 1, 1, 2, 2, 2, 2, 2, 2)
    List(1, 2, 2, 2, 2, 2, 2, 2)
    List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5)
    List(1, 1, 1, 1, 1, 1, 1, 1, 2, 5)
    List(1, 1, 1, 1, 1, 1, 2, 2, 5)
    List(1, 1, 1, 1, 2, 2, 2, 5)
    List(1, 1, 2, 2, 2, 2, 5)
    List(2, 2, 2, 2, 2, 5)
    List(1, 1, 1, 1, 1, 5, 5)
    List(1, 1, 1, 2, 5, 5)
    List(1, 2, 2, 5, 5)
    List(5, 5, 5)
    List(1, 1, 1, 1, 1, 10)
    List(1, 1, 1, 2, 10)
    List(1, 2, 2, 10)
    List(5, 10)
    

    The sumCombinations() function may be used by itself, and the result may be further analyzed to display the "best" solution (the shortest list), or the number of solutions (the number of lists).

    Note that even like this, the requirements may not be fully satisfied. It might happen that the order of each list in the solution be significant. In such a case, each list would have to be duplicated as many time as there are combination of its elements. Or we might be interested only in the combinations that are different.

    For example, we might consider that List(5, 10) should give two combinations: List(5, 10) and List(10, 5). For List(5, 5, 5) it could give three combinations or one only, depending on the requirements. For integers, the three permutations are equivalent, but if we are dealing with coins, like in the "coin changing problem", they are not.

    Also not stated in the requirements is the question of whether each number (or coin) may be used only once or many times. We could (and we should!) generalize the problem to a list of lists of occurrences of each number. This translates in real life into "what are the possible ways to make an certain amount of money with a set of coins (and not a set of coin values)". The original problem is just a particular case of this one, where we have as many occurrences of each coin as needed to make the total amount with each single coin value.

提交回复
热议问题