Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Interesting answers. Thank you for the pointers to wikipedia - whilst interesting - they don't actually solve the problem as stated as I was looking for exact matches - more of an accounting / book blancing problem than a traditional bin-packing / knapsack problem.</p> <p>I have been following the development of stack overflow with interest and wondered how useful it would be. This problem came up at work and I wondered whether stack overflow could provide a ready made answer (or a better answer) quicker than I could write it myself. Thanks also for the comments suggesting this be tagged homework - I guess that is reasonably accurate in light of the above.</p> <p>For those who are interested, here is my solution which uses recursion (naturally) I also changed my mind about the method signature and went for List> rather than decimal[][] as the return type:</p> <pre><code>public class Solver { private List&lt;List&lt;decimal&gt;&gt; mResults; public List&lt;List&lt;decimal&gt;&gt; Solve(decimal goal, decimal[] elements) { mResults = new List&lt;List&lt;decimal&gt;&gt;(); RecursiveSolve(goal, 0.0m, new List&lt;decimal&gt;(), new List&lt;decimal&gt;(elements), 0); return mResults; } private void RecursiveSolve(decimal goal, decimal currentSum, List&lt;decimal&gt; included, List&lt;decimal&gt; notIncluded, int startIndex) { for (int index = startIndex; index &lt; notIncluded.Count; index++) { decimal nextValue = notIncluded[index]; if (currentSum + nextValue == goal) { List&lt;decimal&gt; newResult = new List&lt;decimal&gt;(included); newResult.Add(nextValue); mResults.Add(newResult); } else if (currentSum + nextValue &lt; goal) { List&lt;decimal&gt; nextIncluded = new List&lt;decimal&gt;(included); nextIncluded.Add(nextValue); List&lt;decimal&gt; nextNotIncluded = new List&lt;decimal&gt;(notIncluded); nextNotIncluded.Remove(nextValue); RecursiveSolve(goal, currentSum + nextValue, nextIncluded, nextNotIncluded, startIndex++); } } } } </code></pre> <p>If you want an app to test this works, try this console app code:</p> <pre><code>class Program { static void Main(string[] args) { string input; decimal goal; decimal element; do { Console.WriteLine("Please enter the goal:"); input = Console.ReadLine(); } while (!decimal.TryParse(input, out goal)); Console.WriteLine("Please enter the elements (separated by spaces)"); input = Console.ReadLine(); string[] elementsText = input.Split(' '); List&lt;decimal&gt; elementsList = new List&lt;decimal&gt;(); foreach (string elementText in elementsText) { if (decimal.TryParse(elementText, out element)) { elementsList.Add(element); } } Solver solver = new Solver(); List&lt;List&lt;decimal&gt;&gt; results = solver.Solve(goal, elementsList.ToArray()); foreach(List&lt;decimal&gt; result in results) { foreach (decimal value in result) { Console.Write("{0}\t", value); } Console.WriteLine(); } Console.ReadLine(); } } </code></pre> <p>I hope this helps someone else get their answer more quickly (whether for homework or otherwise).</p> <p>Cheers...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload