【问题标题】:Sum all combinations Excel or Google Spreadsheets汇总所有组合 Excel 或 Google 电子表格
【发布时间】:2018-02-07 13:21:33
【问题描述】:

我需要创建一个电子表格,我将在其中输入几个 (17) 个不同的数字,并在它们之间包含所有可能的总和组合。比如1,2,3,4.....:

1+2
1+3
1+4
2+3
2+4
3+4
.....

如果我也能知道组合来自哪两个数字,那就太好了。

【问题讨论】:

  • 我相信如果不使用宏这是不可能的,因为输出的大小取决于输入的大小。此外,您的任务看起来不像是一个现实的最终问题。所以如果这真的是XY problem,你应该描述你真正的问题。
  • 可能也值得看看这个答案(对于谷歌表格)stackoverflow.com/questions/47922267/…。尽管它显示了字符串的组合,但只需稍加修改即可用于求和。

标签: excel math google-sheets


【解决方案1】:

如果 A2:A18 有 17 个数字,

B1(标题):

=TRANSPOSE(A2:A18)

B2:

=ARRAYFORMULA(A2:A18+TRANSPOSE(A2:A18))

这将给出所有不同组合的 17*17 表:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

【讨论】:

  • 从技术上讲,这是所有对的排列(例如,1+2 被认为与2+1不同)而不是所有对的组合(例如1+2 被认为是与2+1相同)。因此,您的结果是预期的两倍。不过,仍然可能符合 OP 的需求。
【解决方案2】:

如果您只需要值对,请使用这个更简单的宏:

Sub PairsOnly()
    Dim Items(1 To 17) As Variant
    Dim i As Long, j As Long, k As Long
    Dim lower As Long, upper As Long
    lower = LBound(Items)
    upper = UBound(Items)
    k = 2

    For i = lower To upper
        Items(i) = Cells(1, i)
    Next i

    For i = lower To upper - 1
        For j = i + 1 To upper
            Cells(k, 1) = Items(i) & "," & Items(j)
            Cells(k, 2) = Items(i) + Items(j)
            k = k + 1
        Next j
    Next i
End Sub

【讨论】:

    【解决方案3】:

    这适用于使用 VBA

    的 Excel

    A1Q1 中列出您的 17 个值。然后运行这个宏:

    Option Explicit
    
    Sub ListSubsets()
        Dim Items(1 To 17) As Variant
        Dim CodeVector() As Integer
        Dim i As Long, kk As Long
        Dim lower As Long, upper As Long
        Dim NewSub As String
        Dim done As Boolean
        Dim OddStep As Boolean
        kk = 3
        OddStep = True
        lower = LBound(Items)
        upper = UBound(Items)
        For i = lower To upper
            Items(i) = Cells(1, i)
        Next i
    
        ReDim CodeVector(lower To upper) 'it starts all 0
        Application.ScreenUpdating = False
        Do Until done
            'Add a new subset according to current contents
            'of CodeVector
    
            NewSub = ""
            For i = lower To upper
                If CodeVector(i) = 1 Then
                    If NewSub = "" Then
                        NewSub = "'=" & Items(i)
                    Else
                        NewSub = NewSub & "+" & Items(i)
                    End If
                End If
            Next i
            If NewSub = "" Then NewSub = "{}" 'empty set
            Cells(kk, 2) = NewSub
            Cells(kk, 3).Formula = Mid(NewSub, 2)
            kk = kk + 1
            'now update code vector
            If OddStep Then
                'just flip first bit
                CodeVector(lower) = 1 - CodeVector(lower)
            Else
                'first locate first 1
                i = lower
                Do While CodeVector(i) <> 1
                    i = i + 1
                Loop
                'done if i = upper:
                If i = upper Then
                    done = True
                Else
                    'if not done then flip the *next* bit:
                    i = i + 1
                    CodeVector(i) = 1 - CodeVector(i)
                End If
            End If
            OddStep = Not OddStep 'toggles between even and odd steps
        Loop
        Application.ScreenUpdating = True
    End Sub
    

    组合将从 B4 向下显示,相关的总和显示在 D 列中:

    改编自John Coleman's code

    Post

    注意:

    在我的旧笔记本电脑上运行大约需要 4 分钟。

    【讨论】:

    • 除非我有误解,否则 OP 想要列表中所有数字对的总和,这将大大减少这里的结果数量。不过,这正是我解释问题的方式。
    • @ImaginaryHuman072889 你可能是对的............我会想出一个替代答案............
    猜你喜欢
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多