I need to get all the possible combinations of coins available to pay a ceiling price, but limiting the amount of coins to 3 units maximum of each value (to pay € 10 it does not cost 10 coins of € 1).
With the following code I get a combination limited to 3 units per coin, but how can I modify the code to print all possible combinations?
class Program
{
static int precio = 450;
static void Main(string[] args)
{
Moneda[] c = new Moneda[] { new Moneda(300, 3), new Moneda(150, 3), new Moneda(120, 3) ,
new Moneda(100, 3), new Moneda(75, 3), new Moneda(60, 3),
new Moneda(36, 3), new Moneda(20, 3), new Moneda(18, 3)};
for (int i = 0; i < c.Length; i++)
{
precio -= c[i].precio2(precio);
Console.WriteLine(c[i].ToString());
}
Console.ReadLine();
}
}
class Moneda
{
private int precio;
private int counted;
private int maxNo;
public Moneda(int Monedaprecio, int MonedaMaxNo)
{
this.precio = Monedaprecio;
this.maxNo = MonedaMaxNo;
this.counted = 0;
}
public int precio2(int Precio)
{
int Num = Precio / precio;
if (maxNo == 0)
return 0;
if (maxNo != -1)
if (Num > this.maxNo - this.counted)
Num = maxNo;
this.counted += Num;
return Num * precio;
}
public override string ToString()
{
return string.Format("{0} x {1} (max {2}) ", this.precio.ToString(), this.counted.ToString(), this.maxNo.ToString());
}
}
It must be simple, but I am a beginner programmer and I do not see how to do it. Thanks.