代码之家  ›  专栏  ›  技术社区  ›  Sam Meldrum

从一个大小为n的数字列表中找到另一个数字的算法

  •  12
  • Sam Meldrum  · 技术社区  · 16 年前

    我有一个十进制数(我们叫它 目标 )以及其他十进制数的数组(让我们调用该数组 元素 )我需要找到所有的数字组合 元素 目标的总和。

    我喜欢C(.NET 2.0)中的解决方案,但不管怎样,最好的算法都可能获胜。

    您的方法签名可能如下所示:

    public decimal[][] Solve(decimal goal, decimal[] elements)
    
    6 回复  |  直到 16 年前
        1
  •  11
  •   Sam Meldrum    16 年前

    public class Solver {
    
        private List<List<decimal>> mResults;
    
        public List<List<decimal>> Solve(decimal goal, decimal[] elements) {
    
            mResults = new List<List<decimal>>();
            RecursiveSolve(goal, 0.0m, 
                new List<decimal>(), new List<decimal>(elements), 0);
            return mResults; 
        }
    
        private void RecursiveSolve(decimal goal, decimal currentSum, 
            List<decimal> included, List<decimal> notIncluded, int startIndex) {
    
            for (int index = startIndex; index < notIncluded.Count; index++) {
    
                decimal nextValue = notIncluded[index];
                if (currentSum + nextValue == goal) {
                    List<decimal> newResult = new List<decimal>(included);
                    newResult.Add(nextValue);
                    mResults.Add(newResult);
                }
                else if (currentSum + nextValue < goal) {
                    List<decimal> nextIncluded = new List<decimal>(included);
                    nextIncluded.Add(nextValue);
                    List<decimal> nextNotIncluded = new List<decimal>(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(' ');
            List<decimal> elementsList = new List<decimal>();
            foreach (string elementText in elementsText) {
                if (decimal.TryParse(elementText, out element)) {
                    elementsList.Add(element);
                }
            }
    
            Solver solver = new Solver();
            List<List<decimal>> results = solver.Solve(goal, elementsList.ToArray());
            foreach(List<decimal> result in results) {
                foreach (decimal value in result) {
                    Console.Write("{0}\t", value);
                }
                Console.WriteLine();
            }
    
    
            Console.ReadLine();
        }
    }
    

        2
  •  3
  •   Steve Jessop    16 年前

        3
  •  3
  •   Rob Dickerson    16 年前
        4
  •  2
  •   ARKBAN    16 年前
        5
  •  2
  •   Adi    16 年前

        6
  •  -1
  •   takrl cck    13 年前
    public class Logic1 {
        static int val = 121;
        public static void main(String[] args)
        {
            f(new int[] {1,4,5,17,16,100,100}, 0, 0, "{");
        }
    
        static void f(int[] numbers, int index, int sum, String output)
        {
            System.out.println(output + " } = " + sum);
            //System.out.println("Index value1 is "+index);
            check (sum);
            if (index == numbers.length)
            {
                System.out.println(output + " } = " + sum);
                return;
            }
    
            // include numbers[index]
            f(numbers, index + 1, sum + numbers[index], output + " " + numbers[index]);
            check (sum);
            //System.out.println("Index value2 is "+index);
            // exclude numbers[index]
            f(numbers, index + 1, sum, output);
            check (sum);
        }
    
        static void check (int sum1)
        {
            if (sum1 == val)
                System.exit(0);
        }
    }