如何在VB中从数组元素中得出所有可能的总和组合

发布于 2024-08-26 01:38:01 字数 175 浏览 7 评论 0原文

如果有一个数组,其中元素为:1,2,3,4,则程序应返回另一个数组,其中包含所有组合的总和:

1
2
3
4
3 (1+2)
4 (1+3) 
5 (1+4)
5 (2+3)
6 (2+4)
7 (3+4)
6 (1+2+3)
7 (1+2+4)
8 (1+3+4)
9 (2+3+4)
10 (1+2+3+4)

If there is an array with elements: 1,2,3,4, the program should return another array with sum of all combinations:

1
2
3
4
3 (1+2)
4 (1+3) 
5 (1+4)
5 (2+3)
6 (2+4)
7 (3+4)
6 (1+2+3)
7 (1+2+4)
8 (1+3+4)
9 (2+3+4)
10 (1+2+3+4)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南汐寒笙箫 2024-09-02 01:38:01

这是我不久前编写的一个函数,用于生成给定数组的所有可能的子集。它是通用的,所以它支持整数、双精度数、字符串等。

原始的C#

public static List<T[]> CreateSubsets<T>(T[] originalArray)
{
    List<T[]> subsets = new List<T[]>();

    for (int i = 0; i < originalArray.Length; i++)
    {
        int subsetCount = subsets.Count;
        subsets.Add(new T[] { originalArray[i] });

        for (int j = 0; j < subsetCount; j++)
        {
            T[] newSubset = new T[subsets[j].Length + 1];
            subsets[j].CopyTo(newSubset, 0);
            newSubset[newSubset.Length - 1] = originalArray[i];
            subsets.Add(newSubset);
        }
    }

    return subsets;
}

和我刚刚转换为VB 的版本。

Function CreateSubsets(Of T)(ByVal originalArray() As T) As List(Of T())

    Dim subsets As New List(Of T())

    For i As Integer = 0 To originalArray.Length - 1

        Dim subsetCount As Integer = subsets.Count
        subsets.Add(New T() {originalArray(i)})

        For j As Integer = 0 To subsetCount - 1
            Dim newSubset(subsets(j).Length) As T
            subsets(j).CopyTo(newSubset, 0)
            newSubset(newSubset.Length - 1) = originalArray(i)
            subsets.Add(newSubset)
        Next

    Next

    Return subsets

End Function

可以这样食用

    Dim array() As Integer = {1, 2, 3, 4, 5}
    Dim subsets As List(Of Integer()) = CreateSubsets(array)

    For Each subset As Integer() In subsets

        Dim sum As Integer = subset.Sum()

    Next

This is a function I wrote some time ago to generate all possible subsets of a given array. It's generic, so it supports integers, doubles, strings, etc.

Original C#

public static List<T[]> CreateSubsets<T>(T[] originalArray)
{
    List<T[]> subsets = new List<T[]>();

    for (int i = 0; i < originalArray.Length; i++)
    {
        int subsetCount = subsets.Count;
        subsets.Add(new T[] { originalArray[i] });

        for (int j = 0; j < subsetCount; j++)
        {
            T[] newSubset = new T[subsets[j].Length + 1];
            subsets[j].CopyTo(newSubset, 0);
            newSubset[newSubset.Length - 1] = originalArray[i];
            subsets.Add(newSubset);
        }
    }

    return subsets;
}

And the version I just converted to VB.

Function CreateSubsets(Of T)(ByVal originalArray() As T) As List(Of T())

    Dim subsets As New List(Of T())

    For i As Integer = 0 To originalArray.Length - 1

        Dim subsetCount As Integer = subsets.Count
        subsets.Add(New T() {originalArray(i)})

        For j As Integer = 0 To subsetCount - 1
            Dim newSubset(subsets(j).Length) As T
            subsets(j).CopyTo(newSubset, 0)
            newSubset(newSubset.Length - 1) = originalArray(i)
            subsets.Add(newSubset)
        Next

    Next

    Return subsets

End Function

It can be consumed in this manner

    Dim array() As Integer = {1, 2, 3, 4, 5}
    Dim subsets As List(Of Integer()) = CreateSubsets(array)

    For Each subset As Integer() In subsets

        Dim sum As Integer = subset.Sum()

    Next
风尘浪孓 2024-09-02 01:38:01

我的想法是:(

伪代码,我不懂VB)

for(int i = 0; i < 4321; i++)
{
    i mod 10 + // first from right digit
    (int)((i mod 100)mod 10) // second, (?)
    // etc
    // sum up all 4 digit
    // add to array  
}

My idea is:

(pseudcode, I don;t know VB)

for(int i = 0; i < 4321; i++)
{
    i mod 10 + // first from right digit
    (int)((i mod 100)mod 10) // second, (?)
    // etc
    // sum up all 4 digit
    // add to array  
}
甜心 2024-09-02 01:38:01

使用伪 VB 代码对您在评论中提到的算法进行编码:

ReDim result(2 ^ (Length of Array) - 1)
for index = 0 to 2 ^ (Length of Array) - 1
  sum = 0
  for counter = 0 to (Length of Array) - 1
    If ((2 ^ counter) And index) <> 0 Then
      sum += Array(counter+1)

  result(index) = sum

Coding the algorithm you mentioned in your comment, in pseudo VB code:

ReDim result(2 ^ (Length of Array) - 1)
for index = 0 to 2 ^ (Length of Array) - 1
  sum = 0
  for counter = 0 to (Length of Array) - 1
    If ((2 ^ counter) And index) <> 0 Then
      sum += Array(counter+1)

  result(index) = sum
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文