I have seen many answered questions about this kind of topics, but I need to know what is the best way to limit the number of possible combinations given a list of values (1,5,10,20)
and a limit number (100).
My question is, how can I do to avoid results like (100*1 or 20*5)
and select only results with a limit of 10 units (4*20+4*5)
or (3*20+7*5)
or (9*10+1*5)
.
Unwanted combinations:
(20*5), (100*1), (15*5+25*1), (40*1+12*5), etc
Desired combinations (equal or less than 100):
(3*20+7*5), (8*10+1*20), (4*20+1*10+2*5), etc
What i want is all possible combinations under or equal 100 and, (supposing to be coins), combinations with no more than 10 coins.
This piece of code solves the problem of limiting results, but shows only one result:
class Program
{
static int amount = 1000;
static void Main(string[] args)
{
Coin[] c = new Coin[] { new Coin(500, 3), new Coin(200, 3), new Coin(100, 3) ,
new Coin(50, 3), new Coin(20, 3), new Coin(10, 3),
new Coin(5, 3), new Coin(2, 3), new Coin(1, 3)};
int netAmount = amount;
for (int i = 0; i < c.Length; i++)
{
amount -= c[i].coveredPrice(amount);
}
for (int i = 0; i < c.Length; i++)
{
Console.WriteLine(c[i].ToString());
}
Console.ReadLine();
}
}
class Coin
{
private int price;
private int counted;
private int maxNo;
public Coin(int coinPrice, int coinMaxNo)
{
this.price = coinPrice;
this.maxNo = coinMaxNo;
this.counted = 0;
}
public int coveredPrice(int Price)
{
int Num = Price / price;
if (maxNo == 0)
return 0;
if (maxNo != -1)
if (Num > this.maxNo - this.counted)
Num = maxNo;
this.counted += Num;
return Num * price;
}
public override string ToString()
{
return string.Format("{0} x {1} (max {2}) ", this.price.ToString(), this.counted.ToString(), this.maxNo.ToString());
}
}
}
What should i have to modify to show me all the results?