algorithm - What is the most efficient way to get all combinations? -


possible duplicate:
finding possible combinations of numbers reach given sum

problem

you have list of people input, each person has number of credits. given total has achieved using 1 or more persons credits. function should output combinations exist result in 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); // function in question } public class person {     public string name { get; set; }     public int credits { get; set; } } 

in above example, given total of 10, printallcombinations function should output 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: forgot mention, can use part of persons credits

the example used simplified, real scenario constrained having no more 100 people , total less 90.

what best solution?

you can read solutions ubiquitous problem in python, java or haskell here

finding possible combinations of numbers reach given sum


based on edit specifying may take partial credits, should @ post:

generate list of combinations/sets of numbers limited supplies

my solution, written in c, equally applicable case.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -