Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
excelvba的组合算法_Excel_Algorithm_Vba_Combinations - Fatal编程技术网

excelvba的组合算法

excelvba的组合算法,excel,algorithm,vba,combinations,Excel,Algorithm,Vba,Combinations,假设我有一组项目,如下所示: Item Units Apples 5 Pears 5 Carrots 1 Oranges 4 我有六个人可以给他们这些东西。我们叫他们: Mr. A Mr. B Mr. C Mr. D Mr. E Mr. F 我将需要一个Excel VBA代码,检索我的所有可能的组合分享上述项目给不同的人。请注意,(i)订单并不重要,(ii)一个人可能拥有同一物品的多个单位 为了报告这一信息,我考虑了一种类似于以下所示的结构:

假设我有一组项目,如下所示:

Item    Units
Apples    5
Pears     5
Carrots   1
Oranges   4
我有六个人可以给他们这些东西。我们叫他们:

Mr. A
Mr. B
Mr. C
Mr. D
Mr. E
Mr. F
我将需要一个Excel VBA代码,检索我的所有可能的组合分享上述项目给不同的人。请注意,(i)订单并不重要,(ii)一个人可能拥有同一物品的多个单位

为了报告这一信息,我考虑了一种类似于以下所示的结构:

                          Mr A                                 Mr B                   ...
             Apples  Pears  Carrots  Oranges       Apples  Pears  Carrots  Oranges    ...
Scenario 1     1       0       1        2             2      2       0        0       ...
Scenario 2     1       0       1        1             2      2       0        0       ...
...
我知道组合的数量将是巨大的,但不介意计算需求

我一直在试图找出算法,但一直未能实现


提前感谢,

我尝试使用Mr A和Mr B进行测试,共有360种不同的场景

Sub distr_list()
Dim apple_a, apple_b

Count = 1

Cells(Count, 4) = "apple_a"
Cells(Count, 5) = "pear_a"
Cells(Count, 6) = "carrot_a"
Cells(Count, 7) = "orange_a"

Cells(Count, 9) = "apple_b"
Cells(Count, 10) = "pear_b"
Cells(Count, 11) = "carrot_b"
Cells(Count, 12) = "orange_b"

For apple_a = 0 To 5
For apple_b = 0 To 5

For pear_a = 0 To 5
For pear_b = 0 To 5

For carrot_a = 0 To 1
For carrot_b = 0 To 1

For orange_a = 0 To 4
For orange_b = 0 To 4


 If apple_a + apple_b = 5 Then
    If pear_a + pear_b = 5 Then
        If carrot_a + carrot_b = 1 Then
            If orange_a + orange_b = 4 Then

    Cells(Count + 1, 4) = apple_a
    Cells(Count + 1, 5) = pear_a
    Cells(Count + 1, 6) = carrot_a
    Cells(Count + 1, 7) = orange_a

    Cells(Count + 1, 9) = apple_b
    Cells(Count + 1, 10) = pear_b
    Cells(Count + 1, 11) = carrot_b
    Cells(Count + 1, 12) = orange_b

    Count = Count + 1

 End If
 End If
 End If
 End If

Next
Next
Next
Next
Next
Next
Next
Next

End Sub

您可以对其余部分应用相同的逻辑

您可能已经知道这一点,但是如果您发布您尝试过的代码,您将得到更多的帮助。如果你还没有,读一读。如果您没有提供您的编码工作以及它们失败的地方,那么问题很可能就解决了。问题是,我以前只是通过Excel公式实现了类似的组合算法。然而,在这种情况下,我不知道如何处理这个事实,我可能有多个单位给同一个人,我也没有看到任何以前的职位,其中提出了这个问题。什么是一个人可以拥有的每个项目的最大数量?每个人能拥有的所有物品是否有一个最大的总数?如果答案是否定的,那么答案是有无限多的可能组合,因此循环无限。每个人可以拥有的每个物品的最大数量受到该物品的单位数量的限制。例如,A先生可以有5个苹果,而其他人没有。一个人可能拥有的最大物品数量没有任何限制。