Count the coins: Difference between revisions
Content added Content deleted
Line 3,849: | Line 3,849: | ||
<syntaxhighlight lang="go"> |
<syntaxhighlight lang="go"> |
||
fn main() { |
fn main() { |
||
amount := |
amount := 100 |
||
coins := [ |
coins := [25, 10, 5, 1] |
||
println("amount: $amount; ways to make change: ${count_2(coins, amount)}") |
println("amount: $amount; ways to make change: ${count_2(coins, amount)}") |
||
} |
} |
||
fn count(coins []int, amount int) int { |
fn count(coins []int, amount int) int { |
||
mut |
mut ways := []int{len: amount + 1} |
||
ways[0] = 1 |
|||
for coin in coins { |
for coin in coins { |
||
for idx := coin; idx <= amount; idx++ { |
for idx := coin; idx <= amount; idx++ { |
||
ways[idx] += ways[idx - coin] |
|||
} |
} |
||
} |
} |
||
return |
return ways[amount] |
||
} |
} |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
amount: |
amount: 100; ways to make change: 242 |
||
</pre> |
</pre> |
||