Possible Duplicate:
Finding all possible combinations of numbers to reach a given sum
Problem
You have a list of people as input, each person has a number of credits. You are given a total that has to be achieved by using one or more persons credits. The function should output all combinations that exist that will result in the total.
For example see below:-
static void Main(string[] args)
{
int total = Convert.ToInt32(args[0]);
var persons = new List<Person>();
persons.Add(new Person { Credits = 10, Name="Steve"});
persons.Add(new Person { Credits = 5, Name = "Paul" });
persons.Add(new Person { Credits = 4, Name = "David" });
persons.Add(new Person { Credits = 1, Name = "Kenneth" });
PrintAllCombinations(persons, total); // The function in question
}
public class Person
{
public string Name { get; set; }
public int Credits { get; set; }
}
In the above example, given a total of 10, the PrintAllCombinations function should output something similar to:-
- Combination 1) Steve:10
- Combination 2) Paul: 5, David 4, Kenneth 1
- Combination 3) Steve: 9, Paul 1
- Combination 4) Steve: 5, Paul 5
- ...etc
UPDATE: I forgot to mention, you can use any part of a persons credits
The example I used is simplified, but the real scenario would be constrained to having no more than a 100 people and the total will always be less than 90.
What is the best solution?