CSDN博客

img Dannys

算24程序VB部分源码

发表于2004/10/19 21:29:00  519人阅读

Public Function 算24(Card1 As Integer, Card2 As Integer, _
Card3 As Integer, Card4 As Integer) As String
If Card1 < 1 Or Card1 > 13 Then
    MsgBox "无法计算!", 48
    Exit Function
ElseIf Card2 < 1 Or Card2 > 13 Then
    MsgBox "无法计算!", 48
    Exit Function
ElseIf Card3 < 1 Or Card3 > 13 Then
    MsgBox "无法计算!", 48
    Exit Function
ElseIf Card4 < 1 Or Card4 > 13 Then
    MsgBox "无法计算!", 48
    Exit Function
End If

Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim k As Integer

a = Card1
b = Card2
c = Card3
d = Card4

If a + b + c + d = 24 Then
    算24 = Str$(a) + "+" + Str$(b) + "+" + Str$(c) + "+" + Str$(d) + "=24"
    Exit Function
End If
For i = 1 To 2
    For j = 1 To 3
        For n = 1 To 4
            If a + b + c - d = 24 Then
                算24 = Str$(a) + "+" + Str$(b) + "+" + Str$(c) + "-" + Str$(d) + "=24"
                Exit Function
            End If
            If a + b - c - d = 24 Then
                算24 = Str$(a) + "+" + Str$(b) + "-" + Str$(c) + "-" + Str$(d) + "=24"
                Exit Function
            End If
            If a + b + c * d = 24 Then
                算24 = Str$(a) + "+" + Str$(b) + "+" + Str$(c) + "*" + Str$(d) + "=24"
                Exit Function
            End If
            If (a + b + c) * d = 24 Then
                算24 = "(" + Str$(a) + "+" + Str$(b) + "+" + Str$(c) + ")*" + Str$(d) + "=24"
                Exit Function
            End If
            If a + (b + c) * d = 24 Then
                算24 = Str$(a) + "+(" + Str$(b) + "+" + Str$(c) + ")*" + Str$(d) + "=24"
                Exit Function
            End If
            If a + b + c / d = 24 Then
                算24 = Str$(a) + "+" + Str$(b) + "+" + Str$(c) + "/" + Str$(d) + "=24"
                Exit Function
            End If
            If a + (b + c) / d = 24 Then
                算24 = Str$(a) + "+(" + Str$(b) + "+" + Str$(c) + ")/" + Str$(d) + "=24"
                Exit Function
            End If
            If (a + b - c) * d = 24 Then
                算24 = "(" + Str$(a) + "+" + Str$(b) + "-" + Str$(c) + ")*" + Str$(d) + "=24"
                Exit Function
            End If
            If a + (b - c) * d = 24 Then
                算24 = Str$(a) + "+(" + Str$(b) + "-" + Str$(c) + ")*" + Str$(d) + "=24"
                Exit Function
            End If
            If a + b - c / d = 24 Then
                算24 = Str$(a) + "+" + Str$(b) + "-" + Str$(c) + "/" + Str$(d) + "=24"
                Exit Function
            End If
            If (a + b) * (c + d) = 24 Then
                算24 = "(" + Str$(a) + "+" + Str$(b) + ")*(" + Str$(c) + "+" + Str$(d) + ")=24"
                Exit Function
            End If
            If a + b * c - d = 24 Then
                算24 = Str$(a) + "+" + Str$(b) + "*" + Str$(c) + "-" + Str$(d) + "=24"
                Exit Function
            End If
            If (a + b) * c - d = 24 Then
                算24 = "(" + Str$(a) + "+" + Str$(b) + ")*" + Str$(c) + "-" + Str$(d) + "=24"
                Exit Function
            End If
            If (a + b) * (c - d) = 24 Then
                算24 = "(" + Str$(a) + "+" + Str$(b) + ")*(" + Str$(c) + "-" + Str$(d) + ")=24"
                Exit Function
            End If
            If a + b * c * d = 24 Then
                算24 = Str$(a) + "+" + Str$(b) + "*" + Str$(c) + "*" + Str$(d) + "=24"
                Exit Function
            End If
            If (a + b) * c * d = 24 Then
                算24 = "(" + Str$(a) + "+" + Str$(b) + ")*" + Str$(c) + "*" + Str$(d) + "=24"
                Exit Function
            End If
            If (a + b * c) * d = 24 Then
                算24 = "(" + Str$(a) + "+" + Str$(b) + "*" + Str$(c) + ")*" + Str$(d) + "=24"
                Exit Function
            End If
            If a + b * c / d = 24 Then
                算24 = Str$(a) + "+" + Str$(b) + "*" + Str$(c) + "/" + Str$(d) + "=24"
                Exit Function
            End If
            If (a + b) * c / d = 24 Then
                算24 = "(" + Str$(a) + "+" + Str(b) + ")*" + Str$(c) + "/" + Str$(d) + "=24"
                Exit Function
            End If
            If (a + b * c) / d = 24 Then
                算24 = "(" + Str$(a) + "+" + Str$(b) + "*" + Str$(c) + ")/" + Str$(d) + "=24"
                Exit Function
            End If
            If (a + b / c) * d = 24 Then
                算24 = "(" + Str$(a) + "+" + Str$(b) + "/" + Str$(c) + ")*" + Str$(d) + "=24"
                Exit Function
            End If
            If (a - b - c) * d = 24 Then
                算24 = "(" + Str$(a) + "-" + Str$(b) + "-" + Str$(c) + ")*" + Str$(d) + "=24"
                Exit Function
            End If
            If (a - b) * c - d = 24 Then
                算24 = "(" + Str$(a) + "-" + Str$(b) + ")*" + Str$(c) + "-" + Str$(d) + "=24"
                Exit Function
            End If
            If (a - b) * (c - d) = 24 Then
                算24 = "(" + Str$(a) + "-" + Str$(b) + ")*(" + Str$(c) + "-" + Str$(d) + ")=24"
                Exit Function
            End If
            If (a - b) * c * d = 24 Then
                算24 = "(" + Str$(a) + "-" + Str$(b) + ")*" + Str$(c) + "*" + Str$(d) + "=24"
                Exit Function
            End If
            If (a - b * c) * d = 24 Then
                算24 = "(" + Str$(a) + "-" + Str$(b) + "*" + Str$(c) + ")*" + Str$(d) + "=24"
                Exit Function
            End If
            If (a - b) * c / d = 24 Then
                算24 = "(" + Str$(a) + "-" + Str$(b) + ")*" + Str$(c) + "/" + Str$(d) + "=24"
                Exit Function
            End If
            If (a - b / c) * d = 24 Then
                算24 = "(" + Str$(a) + "-" + Str$(b) + "/" + Str$(c) + ")*" + Str$(d) + "=24"
                Exit Function
            End If
            If a * b + c * d = 24 Then
                算24 = Str$(a) + "*" + Str$(b) + "+" + Str$(c) + "*" + Str$(d) + "=24"
                Exit Function
            End If
            If a * b + c / d = 24 Then
                算24 = Str$(a) + "*" + Str$(b) + "+" + Str$(c) + "/" + Str$(d) + "=24"
                Exit Function
            End If
            If a * b - c - d = 24 Then
                算24 = Str$(a) + "*" + Str$(b) + "-" + Str$(c) + "-" + Str$(d) + "=24"
                Exit Function
            End If
            If a * b - c * d = 24 Then
                算24 = Str$(a) + "*" + Str$(b) + "-" + Str$(c) + "*" + Str$(d) + "=24"
                Exit Function
            End If
            If (a * b - c) * d = 24 Then
                算24 = "(" + Str$(a) + "*" + Str$(b) + "-" + Str$(c) + ")*" + Str$(d) + "=24"
                Exit Function
            End If
            If a * b - c / d = 24 Then
                算24 = Str$(a) + "*" + Str$(b) + "-" + Str$(c) + "/" + Str$(d) + "=24"
                Exit Function
            End If
            If (a * b - c) / d = 24 Then
                算24 = "(" + Str$(a) + "*" + Str$(b) + "-" + Str$(c) + ")/" + Str$(d) + "=24"
                Exit Function
            End If
            If a * b * c - d = 24 Then
                算24 = Str$(a) + "*" + Str$(b) + "*" + Str$(c) + "-" + Str$(d) + "=24"
                Exit Function
            End If
            If a * b * c * d = 24 Then
                算24 = Str$(a) + "*" + Str$(b) + "*" + Str$(c) + "*" + Str$(d) + "=24"
                Exit Function
            End If
            If a * b * c / d = 24 Then
                算24 = Str$(a) + "*" + Str$(b) + "*" + Str$(c) + "/" + Str$(d) + "=24"
                Exit Function
            End If
            If a * b / (c + d) = 24 Then
                算24 = Str$(a) + "*" + Str$(b) + "/(" + Str$(c) + "+" + Str$(d) + ")=24"
                Exit Function
            End If
            If a * b / c - d = 24 Then
                算24 = Str$(a) + "*" + Str$(b) + "/" + Str$(c) + "-" + Str$(d) + "=24"
                Exit Function
            End If
            If c = d Then
            ElseIf a * b / (c - d) = 24 Then
                算24 = Str$(a) + "*" + Str$(b) + "/(" + Str$(c) + "-" + Str$(d) + ")=24"
                Exit Function
            End If
            If a * (b / c - d) = 24 Then
                算24 = Str$(a) + "*(" + Str$(b) + "/" + Str$(c) + "-" + Str$(d) + ")=24"
                Exit Function
            End If
            If a * b / c / d = 24 Then
                算24 = Str$(a) + "*" + Str$(b) + "/" + Str$(c) + "/" + Str$(d) + "=24"
                Exit Function
            End If
            If b = c / d Then
            ElseIf a / (b - c / d) = 24 Then
                算24 = Str$(a) + "/(" + Str$(b) + "-" + Str$(c) + "/" + Str$(d) + ")=24"
                Exit Function
            End If
            If b / c = d Then
            ElseIf a / (b / c - d) = 24 Then
                算24 = Str$(a) + "/(" + Str$(b) + "/" + Str$(c) + "-" + Str$(d) + ")=24"
                Exit Function
            End If
        k = a: a = b: b = c: c = d: d = k
        Next n
    k = b: b = c: c = d: d = k
    Next j
k = c: c = d: d = k
Next i
算24 = "本题无解!"

End Function

阅读全文
0 0

相关文章推荐

img
取 消
img