不考虑交换,按二种情况写过了,现在写成了模块。代码如下:
如输入 1 2 3 4
结果为:
Option Explicit
Public Type T24type
s As String
v As Long
End Type
Public Function Test24_2(a As Long, b As Long, c As Long, d As Long) As String
Dim r1(4) As T24type, r2(4) As T24type, r3(4) As T24type
Dim l1 As T24type, l2 As T24type
Dim l3 As T24type, l4 As T24type
Dim i As Long, j As Long, m As Long
Dim s As String
'第一个数
l1.s = CStr(a)
l1.v = a
'第二个数
l2.s = CStr(b)
l2.v = b
'第三个数
l3.s = CStr(c)
l3.v = c
'第四个数
l4.s = CStr(d)
l4.v = d
'第一个数与第二个数计算
Call operation(l1, l2, r1)
'第一分支,第1、2数运算后与第3、4数运算
'第三个数与第四个数计算
Call operation(l3, l4, r2)
For i = 1 To 4
For j = 1 To 4
If r1(i).v >= 0 And r2(i).v >= 0 Then
Call operation(r1(j), r2(i), r3)
For m = 1 To 4
If r3(m).v = 24 Then
s = s & r3(m).s & " = 24" & vbCrLf
End If
Next m
End If
Next j
Next i
'第二分支,第1、2数运算后与第3数运算,再与第4数运算
For i = 1 To 4
If r1(i).v >= 0 Then
Call operation(r1(i), l3, r2)
For j = 1 To 4
If r2(j).v >= 0 Then
Call operation(r2(j), l4, r3)
For m = 1 To 4
If r3(m).v = 24 Then
s = s & r3(m).s & " = 24" & vbCrLf
End If
Next m
End If
Next j
End If
Next i
Test24_2 = s
End Function
Public Sub operation(a As T24type, b As T24type, r() As T24type)
Dim t As Single
'加法
r(1).v = a.v + b.v
r(1).s = "( " & a.s & " + " & b.s & " )"
'减法,不考虑负数
If a.v > b.v Then
r(2).v = a.v - b.v
r(2).s = "( " & a.s & " - " & b.s & " )"
Else
r(2).v = -1
End If
'乘法
r(3).v = a.v * b.v
r(3).s = "( " & a.s & " * " & b.s & " )"
'除法,只允许整除
If a.v >= b.v And b.v > 0 Then
If a.v Mod b.v = 0 Then
r(4).v = a.v / b.v
r(4).s = "( " & a.s & " / " & b.s & " )"
Else
r(4).v = -1
End If
Else
r(4).v = -1
End If
End Sub
Public Type T24type
s As String
v As Long
End Type
Public Function Test24_2(a As Long, b As Long, c As Long, d As Long) As String
Dim r1(4) As T24type, r2(4) As T24type, r3(4) As T24type
Dim l1 As T24type, l2 As T24type
Dim l3 As T24type, l4 As T24type
Dim i As Long, j As Long, m As Long
Dim s As String
'第一个数
l1.s = CStr(a)
l1.v = a
'第二个数
l2.s = CStr(b)
l2.v = b
'第三个数
l3.s = CStr(c)
l3.v = c
'第四个数
l4.s = CStr(d)
l4.v = d
'第一个数与第二个数计算
Call operation(l1, l2, r1)
'第一分支,第1、2数运算后与第3、4数运算
'第三个数与第四个数计算
Call operation(l3, l4, r2)
For i = 1 To 4
For j = 1 To 4
If r1(i).v >= 0 And r2(i).v >= 0 Then
Call operation(r1(j), r2(i), r3)
For m = 1 To 4
If r3(m).v = 24 Then
s = s & r3(m).s & " = 24" & vbCrLf
End If
Next m
End If
Next j
Next i
'第二分支,第1、2数运算后与第3数运算,再与第4数运算
For i = 1 To 4
If r1(i).v >= 0 Then
Call operation(r1(i), l3, r2)
For j = 1 To 4
If r2(j).v >= 0 Then
Call operation(r2(j), l4, r3)
For m = 1 To 4
If r3(m).v = 24 Then
s = s & r3(m).s & " = 24" & vbCrLf
End If
Next m
End If
Next j
End If
Next i
Test24_2 = s
End Function
Public Sub operation(a As T24type, b As T24type, r() As T24type)
Dim t As Single
'加法
r(1).v = a.v + b.v
r(1).s = "( " & a.s & " + " & b.s & " )"
'减法,不考虑负数
If a.v > b.v Then
r(2).v = a.v - b.v
r(2).s = "( " & a.s & " - " & b.s & " )"
Else
r(2).v = -1
End If
'乘法
r(3).v = a.v * b.v
r(3).s = "( " & a.s & " * " & b.s & " )"
'除法,只允许整除
If a.v >= b.v And b.v > 0 Then
If a.v Mod b.v = 0 Then
r(4).v = a.v / b.v
r(4).s = "( " & a.s & " / " & b.s & " )"
Else
r(4).v = -1
End If
Else
r(4).v = -1
End If
End Sub
如输入 1 2 3 4
结果为:
( ( 1 * 2 ) * ( 3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