edvakf
-
Haskell,
pasted
on Aug 19:
|
import List (sort)
solveCoin :: Integer -> [Integer] -> [[Integer]]
solveCoin total coins =
let coins' = reverse $ sort coins
in takeCoins coins' [] total
takeCoins :: [Integer] -> [Integer] -> Integer -> [[Integer]]
takeCoins [] _ _ = []
takeCoins cs xs rest =
let c = head cs
cs' = tail cs
possibleCombinations = case signum (rest - c) of
-1 -> []
0 -> [c:xs]
1 -> takeCoins cs (c:xs) (rest - c)
in possibleCombinations ++ (takeCoins cs' xs rest)
main = print $ solveCoin 10 [1, 5, 10, 50, 100, 500]
|
Output:
|
[[10],[5,5],[1,1,1,1,1,5],[1,1,1,1,1,1,1,1,1,1]]
|
|