我有一个十进制数字(让我们称之为目标)和一个其他十进制数字的数组(让我们调用数组元素)我需要找到总和为目标的元素的所有数字组合.
我倾向于使用C#(.Net 2.0)中的解决方案,但最好的算法可能无论如何都会获胜.
您的方法签名可能类似于:
public decimal[][] Solve(decimal goal, decimal[] elements)
Sam Meldrum.. 11
有趣的答案.感谢您指向维基百科 - 虽然有趣 - 他们实际上并没有解决问题,因为我正在寻找完全匹配 - 更多的会计/书籍问题比传统的bin-packing /背包问题.
我一直关注堆栈溢出的开发,并且想知道它会有多大用处.这个问题出现在工作中,我想知道堆栈溢出是否能够比我自己编写的更快地提供现成的答案(或更好的答案).还要感谢评论提示这是标记的作业 - 我认为鉴于上述情况,这是相当准确的.
对于那些感兴趣的人,这里是我的解决方案,使用递归(自然)我也改变了我的方法签名的思路,并去了List>而不是decimal [] []作为返回类型:
public class Solver { private List> mResults; public List
> Solve(decimal goal, decimal[] elements) { mResults = new List
>(); RecursiveSolve(goal, 0.0m, new List
(), new List (elements), 0); return mResults; } private void RecursiveSolve(decimal goal, decimal currentSum, List included, List notIncluded, int startIndex) { for (int index = startIndex; index < notIncluded.Count; index++) { decimal nextValue = notIncluded[index]; if (currentSum + nextValue == goal) { List newResult = new List (included); newResult.Add(nextValue); mResults.Add(newResult); } else if (currentSum + nextValue < goal) { List nextIncluded = new List (included); nextIncluded.Add(nextValue); List nextNotIncluded = new List (notIncluded); nextNotIncluded.Remove(nextValue); RecursiveSolve(goal, currentSum + nextValue, nextIncluded, nextNotIncluded, startIndex++); } } } }
如果您希望应用测试此功能,请尝试此控制台应用代码:
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(' '); ListelementsList = new List (); foreach (string elementText in elementsText) { if (decimal.TryParse(elementText, out element)) { elementsList.Add(element); } } Solver solver = new Solver(); List > results = solver.Solve(goal, elementsList.ToArray()); foreach(List
result in results) { foreach (decimal value in result) { Console.Write("{0}\t", value); } Console.WriteLine(); } Console.ReadLine(); } }
我希望这有助于其他人更快地得到答案(无论是作业还是其他).
干杯...
有趣的答案.感谢您指向维基百科 - 虽然有趣 - 他们实际上并没有解决问题,因为我正在寻找完全匹配 - 更多的会计/书籍问题比传统的bin-packing /背包问题.
我一直关注堆栈溢出的开发,并且想知道它会有多大用处.这个问题出现在工作中,我想知道堆栈溢出是否能够比我自己编写的更快地提供现成的答案(或更好的答案).还要感谢评论提示这是标记的作业 - 我认为鉴于上述情况,这是相当准确的.
对于那些感兴趣的人,这里是我的解决方案,使用递归(自然)我也改变了我的方法签名的思路,并去了List>而不是decimal [] []作为返回类型:
public class Solver { private List> mResults; public List
> Solve(decimal goal, decimal[] elements) { mResults = new List
>(); RecursiveSolve(goal, 0.0m, new List
(), new List (elements), 0); return mResults; } private void RecursiveSolve(decimal goal, decimal currentSum, List included, List notIncluded, int startIndex) { for (int index = startIndex; index < notIncluded.Count; index++) { decimal nextValue = notIncluded[index]; if (currentSum + nextValue == goal) { List newResult = new List (included); newResult.Add(nextValue); mResults.Add(newResult); } else if (currentSum + nextValue < goal) { List nextIncluded = new List (included); nextIncluded.Add(nextValue); List nextNotIncluded = new List (notIncluded); nextNotIncluded.Remove(nextValue); RecursiveSolve(goal, currentSum + nextValue, nextIncluded, nextNotIncluded, startIndex++); } } } }
如果您希望应用测试此功能,请尝试此控制台应用代码:
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(' '); ListelementsList = new List (); foreach (string elementText in elementsText) { if (decimal.TryParse(elementText, out element)) { elementsList.Add(element); } } Solver solver = new Solver(); List > results = solver.Solve(goal, elementsList.ToArray()); foreach(List
result in results) { foreach (decimal value in result) { Console.Write("{0}\t", value); } Console.WriteLine(); } Console.ReadLine(); } }
我希望这有助于其他人更快地得到答案(无论是作业还是其他).
干杯...