vba程序用7重循环来计算24

 

'添加三件控件,F1单元格存放最大值
Sub Cal()
Dim t As Single
Dim Num(1 To 4)
t = Timer
For i = 1 To 4
    If Sheets(1).CheckBox1.Value Then Cells(1, i) = WorksheetFunction.RandBetween(1, Range("F1"))
    Num(i) = Cells(1, i)
Next
r = Cells(Rows.Count, 1).End(xlUp).Row + 1
Range("A2:B" & r) = ""
Application.ScreenUpdating = False
r = 3
For a = 1 To 4
    For b = 1 To 4
        For c = 1 To 4
            For d = 1 To 4
                For i = 1 To 4
                    For j = 1 To 4
                        For k = 1 To 4
                            If a <> b And a <> c And a <> d And b <> c And b <> d And c <> d Then
                                '无括号的运算
                                Cells(r, 1) = Num(a) & Sign(i) & Num(b) & Sign(j) & Num(c) & Sign(k) & Num(d) & " = "
                                Cells(r, 2) = "=" & Num(a) & Sign(i) & Num(b) & Sign(j) & Num(c) & Sign(k) & Num(d)
                                If Cells(r, 2) = 24 Then r = r + 1
                                '有括号运算(a b)c d
                                Cells(r, 1) = "(" & Num(a) & Sign(i) & Num(b) & ")" & Sign(j) & Num(c) & Sign(k) & Num(d) & " = "
                                Cells(r, 2) = "=" & "(" & Num(a) & Sign(i) & Num(b) & ")" & Sign(j) & Num(c) & Sign(k) & Num(d)
                                If Not IsError(Cells(r, 2)) Then '注意有括号要避免分母为0的情况
                                    If Cells(r, 2) = 24 Then r = r + 1
                                End If
                                '(a b c) d
                                Cells(r, 1) = "(" & Num(a) & Sign(i) & Num(b) & Sign(j) & Num(c) & ")" & Sign(k) & Num(d) & " = "
                                Cells(r, 2) = "=" & "(" & Num(a) & Sign(i) & Num(b) & Sign(j) & Num(c) & ")" & Sign(k) & Num(d)
                                If Not IsError(Cells(r, 2)) Then
                                    If Cells(r, 2) = 24 Then r = r + 1
                                End If
                                'a ( b c) d
                                Cells(r, 1) = Num(a) & Sign(i) & "(" & Num(b) & Sign(j) & Num(c) & ")" & Sign(k) & Num(d) & " = "
                                Cells(r, 2) = "=" & Num(a) & Sign(i) & "(" & Num(b) & Sign(j) & Num(c) & ")" & Sign(k) & Num(d)
                                If Not IsError(Cells(r, 2)) Then
                                    If Cells(r, 2) = 24 Then r = r + 1
                                End If
                                'a (b c d)
                                Cells(r, 1) = Num(a) & Sign(i) & "(" & Num(b) & Sign(j) & Num(c) & Sign(k) & Num(d) & ")" & " = "
                                Cells(r, 2) = "=" & Num(a) & Sign(i) & "(" & Num(b) & Sign(j) & Num(c) & Sign(k) & Num(d) & ")"
                                If Not IsError(Cells(r, 2)) Then
                                    If Cells(r, 2) = 24 Then r = r + 1
                                End If
                                'a b (c d)
                                Cells(r, 1) = Num(a) & Sign(i) & Num(b) & Sign(j) & "(" & Num(c) & Sign(k) & Num(d) & ")" & " = "
                                Cells(r, 2) = "=" & Num(a) & Sign(i) & Num(b) & Sign(j) & "(" & Num(c) & Sign(k) & Num(d) & ")"
                                If Not IsError(Cells(r, 2)) Then
                                    If Cells(r, 2) = 24 Then r = r + 1
                                End If
                                '(a b) (c d)
                                Cells(r, 1) = "(" & Num(a) & Sign(i) & Num(b) & ")" & Sign(j) & "(" & Num(c) & Sign(k) & Num(d) & ")" & " = "
                                Cells(r, 2) = "=" & "(" & Num(a) & Sign(i) & Num(b) & ")" & Sign(j) & "(" & Num(c) & Sign(k) & Num(d) & ")"
                                If Not IsError(Cells(r, 2)) Then
                                    If Cells(r, 2) = 24 Then r = r + 1
                                End If
                            End If
                        Next
                    Next
                Next
            Next
        Next
    Next
Next
If Cells(r, 2) <> 24 Then Range("A" & r & ":B" & r) = ""
r = Cells(Rows.Count, 1).End(xlUp).Row
If r = 1 Then
    Cells(2, 1) = "此四数无解!"
    Exit Sub
Else
    Cells(2, 1) = "共" & r - 2 & "种解法"
End If

'以下去掉重复解法,如不计重新以下代码除最后2行可以全部删除
'只要公式的字串不同即算一种解法,而不考虑交换律、结合律、运算符等级的实质相同。
For j = r To 4 Step -1
    For i = 3 To j - 1
        If Cells(j, 1) = Cells(i, 1) Then
            Cells(j, 1) = ""
            Cells(j, 2) = ""
        End If
    Next
Next
'以下删除空行后重新计算行数
For i = r To 3 Step -1
    If Cells(i, 1) = "" Then
        Rows(i).Delete
    End If
Next
r = Cells(Rows.Count, 1).End(xlUp).Row
Cells(2, 1) = "共" & r - 2 & "种解法"

'计算时间
Cells(2, 1) = Cells(2, 1) & ",耗时" & Timer - t & "秒。"
Application.ScreenUpdating = True

End Sub

Function Sign(x)
Select Case x
    Case 1: Sign = " + "
    Case 2: Sign = " - "
    Case 3: Sign = " * "
    Case 4: Sign = " / "
End Select
End Function

 

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hann Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值