简体   繁体   English

在一组数字中找到总和等于已知数字的组合的有效算法

[英]Efficient algorithm to find a combination, which summation is equal to a known number, in a set of number

Let's say there is a set of number假设有一组数字

1, 2, 3, 4, 5, 6, 7, 8, 9, 10 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

I want to find out several combinations in the set of number such that the summation of it equal to a known number, for example, 18. We can find out that 5, 6, 7 is matched (5+6+7=18).我想找出一组数字中的几种组合,使其总和等于一个已知数字,例如,18。我们可以找到匹配的 5, 6, 7 (5+6+7=18) .

Numbers in a combination cannot be repeated and the number in a set may not be consecutive.组合中的数字不能重复,一组中的数字不能连续。

I've wrote a C# program to do that.我写了一个 C# 程序来做到这一点。 The program is random to pick up number to form a combination and check whether the summation of combination is equal to a known number.程序随机取数组成组合,并检查组合之和是否等于已知数。 However, the combination the program found may be repeated and it makes the progress not effective.但是,程序找到的组合可能会重复,从而使进度无效。

I am wondering whether there is any efficient algorithm to find out such combination.我想知道是否有任何有效的算法来找出这种组合。

Here's part of my code.这是我的代码的一部分。

        int Sum = 0;
        int c;
        List<int> Pick = new List<int>();
        List<int> Target = new List<int>() {some numbers}

        Target.Sort();

            while (!Target.Contains(Sum))
            {
                if (Sum > Target[Target.Count - 1])
                {
                            Pick.Clear();
                            Sum = 0;

                }
                while (true)
                {
                    if (Pick.IndexOf(c = Math0.rand(0, Set.Count - 1)) == -1)
                    {
                        Pick.Add(c);
                    }

                    //Summation Pick
                    Sum = 0;
                    for (int i = 0; i < Pick.Count; i++)
                        Sum += Set[Pick[i]];

                    if (Sum >= Target[Target.Count - 1])
                        break;
                }


            }

            Result.Add(Pick);

You can use recursion.您可以使用递归。 For any given number in the set, find the combinations of smaller numbers that adds up to the number:对于集合中的任何给定数字,找出与该数字相加的较小数字的组合:

public static IEnumerable<string> GetCombinations(int[] set, int sum, string values) {
  for (int i = 0; i < set.Length; i++) {
    int left = sum - set[i];
    string vals = set[i] + "," + values;
    if (left == 0) {
      yield return vals;
    } else {
      int[] possible = set.Take(i).Where(n => n <= sum).ToArray();
      if (possible.Length > 0) {
        foreach (string s in GetCombinations(possible, left, vals)) {
          yield return s;
        }
      }
    }
  }
}

Usage:用法:

int[] set = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

foreach (string s in GetCombinations(set, 18, "")) {
  Console.WriteLine(s);
}

Output:输出:

1,2,4,5,6,
3,4,5,6,
1,2,3,5,7,
2,4,5,7,
2,3,6,7,
1,4,6,7,
5,6,7,
1,2,3,4,8,
2,3,5,8,
1,4,5,8,
1,3,6,8,
4,6,8,
1,2,7,8,
3,7,8,
2,3,4,9,
1,3,5,9,
4,5,9,
1,2,6,9,
3,6,9,
2,7,9,
1,8,9,
1,3,4,10,
1,2,5,10,
3,5,10,
2,6,10,
1,7,10,
8,10,

A possible alternative method.一种可能的替代方法。 With a small set like this, you could use brute force.像这样的小集合,你可以使用蛮力。 Your set {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} has 10 elements, and each element can be present or not present.你的集合{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}有 10 个元素,每个元素可以存在也可以不存在。 That can be mapped to a binary number between 0 (= 0b0000000000) and 1023 (= 0b1111111111).这可以映射到 0 (= 0b0000000000) 和 1023 (= 0b1111111111) 之间的二进制数。 Loop through the numbers from 0 to 1023, inclusive, and check the sum for the subset corresponding to the set bits of the binary representation of the number.循环遍历从 0 到 1023(含)的数字,并检查与该数字的二进制表示的设置位对应的子集的总和。

Maybe not the most useful for this particular question, but a good way to generate all possible subsets of a given set.对于这个特定问题,可能不是最有用的方法,而是生成给定集合的所有可能子集的好方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何构建一个算法来查找一个组合,其总和最接近一个数字,其差异在c#的范围内 - How to build an algorithm to find a combination, which summation is nearest to a number and its difference is within a range in c# 查找具有特定数字集的数字的所有加法组合的算法? - Algorithm to find all addition combination of a number with specific number sets? 如何找到所有子集,这些子集的元素之和等于一个常数? - How to find all subset which is the summation of its elements equals to a constant number? 是否有一种已知算法可以找出 N 个元素中哪些 K 个元素的总和最接近整数? - Is there a known algorithm for finding which K elements out of N elements have a sum that is closest to a whole number? 查找一组数字中每个最大数的最有效方法 - Most efficient way to find every max number in a set of a numbers 根据这些限制查找数字的算法 - Algorithm to find a number following these restrictions 找到最接近目标数字的数字组合 - Find the combination of numbers that closest matches a target number 是否有一种已知的快速算法来查找与给定数字相乘的所有数字对? - Is there a known fast algorithm for finding all pairs of numbers that multiply to a given number? 将ID设置为等于文本框中包含的数字 - set Id equal to number contained in textbox 用于查找大小为n的列表中的哪些数字与另一个数字相加的算法 - Algorithm to find which numbers from a list of size n sum to another number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM