Algorithm to determine coin combinations

后端 未结 13 2651
臣服心动
臣服心动 2020-12-25 08:33

I was recently faced with a prompt for a programming algorithm that I had no idea what to do for. I\'ve never really written an algorithm before, so I\'m kind of a newb at t

13条回答
  •  忘掉有多难
    2020-12-25 09:05

    Recursive solution based on algorithmist.com resource in Scala:

    def countChange(money: Int, coins: List[Int]): Int = {
        if (money < 0 || coins.isEmpty) 0
        else if (money == 0) 1
        else countChange(money, coins.tail) + countChange(money - coins.head, coins)
    }
    

提交回复
热议问题