Buenas, aquí tienes el algoritmo expresado en php (en c# hay que adaptarlo)
Hay que pasarle los valores de cantidad (por ejemplo 100) y de monedas disponibles (por ejemplo 1, 5, 10, 25)
function make_change ($amount, $coins)
{
$coin_count = count($coins);
$table = array();
for ($i = -1; $i <= $amount; $i++) {
for($j = -1; $j <= $coin_count; $j++) {
// Rules
// 1: table[0,0] or table[0,x] = 1
// 2: talbe[i <= -1, x] = 0
// 3: table[x, j <= -1] = 0
$total = 0;
// first sub-problem
// count(n, m-1)
$n = $i;
$m = $j-1;
if ($n == 0) // rule 1
$total += 1;
else if ($n <= -1) // rule 2
$total += 0;
else if (($m <= 0) && ($n >= 1))
$total += 0;
else
$total += $table[$n][$m];
// second sub-problem
// count(n-S[m], m)
if (($j-1) <= -1)
$total += 0;
else {
$n = $i - $coins[$j - 1];
$m = $j;
if ($n == 0) // rule 1
$total += 1;
else if ($n <= -1) // rule 2
$total += 0;
else if (($m <= 0) && ($n >= 1)) // rule 3
$total += 0;
else
$total += $table[$n][$m];
}
$table[$i][$j] = $total;
}
}
return $table[$i-1][$j-1];
}
En C# he encontrado este:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<int> coins = new List<int>();
List<int> amounts = new List<int>() { 1, 5, 10, 25, 50 };
//
// Compute change for 51 cents.
//
Change(coins, amounts, 0, 0, 51);
}
static void Change(List<int> coins, List<int> amounts, int highest, int sum, int goal)
{
//
// See if we are done.
//
if (sum == goal)
{
Display(coins, amounts);
return;
}
//
// See if we have too much.
//
if (sum > goal)
{
return;
}
//
// Loop through amounts.
//
foreach (int value in amounts)
{
//
// Only add higher or equal amounts.
//
if (value >= highest)
{
List<int> copy = new List<int>(coins);
copy.Add(value);
Change(copy, amounts, value, sum + value, goal);
}
}
}
static void Display(List<int> coins, List<int> amounts)
{
foreach (int amount in amounts)
{
int count = coins.Count(value => value == amount);
Console.WriteLine("{0}: {1}",
amount,
count);
}
Console.WriteLine();
}
}
Salidas
Salidas
1: 51
5: 0
10: 0
25: 0
50: 0
1: 46
5: 1
10: 0
25: 0
50: 0
1: 41
5: 2
10: 0
25: 0
50: 0
1: 41
5: 0
10: 1
25: 0
50: 0
1: 36
5: 3
10: 0
25: 0
50: 0