Skip to content

Instantly share code, notes, and snippets.

@wintercn
Last active March 23, 2019 13:33
Show Gist options
  • Select an option

  • Save wintercn/5596588 to your computer and use it in GitHub Desktop.

Select an option

Save wintercn/5596588 to your computer and use it in GitHub Desktop.
现在有1分、2分、5分的硬币各无限枚,要凑成1元有多少种凑法?
function getResult(n,coins) {
var a = new Array(n+1);
for(var i = 0; i <= n;i++)
a[i] = 0;
a[0] = 1;
for(var j = 0; j<coins.length; j++)
for(var i = 0; i <= n;i++)
a[i+coins[j]] += a[i];
return a[n];
}
getResult(100,[1,2,5]); // 541
@hax
Copy link

hax commented May 20, 2013

我厂前端同学的解法(haskell):

module Main where
    coin :: Int -> Int
    coin 0 = 0
    coin n = sum $ map (\a -> (a `div` 2 + 1)) $ takeWhile (>=0) [a | a <- [n - (5 * b) | b <- [0 .. ]]]

详见http://zhanglin.pro/19

@vvianyy
Copy link

vvianyy commented Sep 30, 2013

就是动态规划吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment