|
楼主 |
发表于 2024-12-24 11:02
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
然后把其他功能全部写在主程序里面了。(系统提示文字太长了,只能分成两段了)
Part 1:
- Sub 计算24点()
- On Error Resume Next
- Sheets(1).[c4].Clear
- ' 先将4个数字排序、写入数组arr1
- Dim dic_temp, arr(4), arr1(), str_temp As String
-
- Set dic_temp = CreateObject("scripting.dictionary") ' 创建一个字典,用来消除重复的排序
-
- arr(1) = Int(Sheets(1).[b4].Value)
- arr(2) = Int(Sheets(1).[b5].Value)
- arr(3) = Int(Sheets(1).[b6].Value)
- arr(4) = Int(Sheets(1).[b7].Value)
-
- For i = 1 To 4
- For j = 1 To 4
- If i <> j Then
- For k = 1 To 4
- If k <> i And k <> j Then
- For l = 1 To 4
- If l <> i And l <> j And l <> k Then
-
- ' 检查排序是否已经存在
- str_temp = arr(i) & "," & arr(j) & "," & arr(k) & "," & arr(l)
-
- ' 如果不存在,则写入字典、数组
- If Not dic_temp.exists(str_temp) Then
- dic_temp(str_temp) = str_temp
- r = r + 1
- ReDim Preserve arr1(1 To 4, 1 To r)
-
- arr1(1, r) = arr(i)
- arr1(2, r) = arr(j)
- arr1(3, r) = arr(k)
- arr1(4, r) = arr(l)
- End If
-
- End If
- Next
- End If
- Next
- End If
- Next
- Next
- ' 组合排序完成,为4行、r列
- Dim a1 As Integer, a2 As Integer, a3 As Integer, a4 As Integer ' 临时存储排序后的数据
- Dim num1 As Double, num2 As Double, num3 As Double ' 3次计算出来的结果
- Dim ss1 As String, ss2 As String, ss3 As String ' 用于存储计算中间过程
-
- ' 第一种情况:((a+b)+c)+d
- For loop1 = 1 To UBound(arr1, 2)
- For s1 = 1 To 6 ' 分别代表第一个 + - * /
- num1 = 四则计算(arr1(1, loop1), arr1(2, loop1), s1)
-
- If Err <> 0 Then
- Err.Clear
- GoTo 100
- End If
-
- If num1 < 0 Then GoTo 100
-
- For s2 = 1 To 6
- num2 = 四则计算(num1, arr1(3, loop1), s2)
-
- If Err <> 0 Then
- Err.Clear
- GoTo 200
- End If
-
- If num2 < 0 Then GoTo 200
-
- For s3 = 1 To 6
- num3 = 四则计算(num2, arr1(4, loop1), s3)
-
- If Err <> 0 Then
- Err.Clear
- GoTo 300
- End If
-
- If num3 < 0 Then GoTo 300
-
- If Abs(num3 - 24) < 0.0001 Then
- a1 = arr1(1, loop1)
- a2 = arr1(2, loop1)
- a3 = arr1(3, loop1)
- a4 = arr1(4, loop1)
-
- ss1 = Switch(s1 = 1, a1 & "+" & a2, _
- s1 = 2, a1 & "-" & a2, _
- s1 = 3, a1 & "×" & a2, _
- s1 = 4, a1 & "÷" & a2, _
- s1 = 5, a2 & "-" & a1, _
- s1 = 6, a2 & "÷" & a1)
- ss2 = Switch(s2 = 1, ss1 & "+" & a3, _
- s2 = 2, ss1 & "-" & a3, _
- s2 = 3 And (s1 - 1) * (s1 - 2) * (s1 - 5) = 0, "(" & ss1 & ")×" & a3, _
- s2 = 3 And (s1 - 1) * (s1 - 2) * (s1 - 5) <> 0, ss1 & "×" & a3, _
- s2 = 4 And (s1 - 1) * (s1 - 2) * (s1 - 5) = 0, "(" & ss1 & ")÷" & a3, _
- s2 = 4 And (s1 - 1) * (s1 - 2) * (s1 - 5) <> 0, ss1 & "÷" & a3, _
- s2 = 5 And (s1 - 1) * (s1 - 2) * (s1 - 5) = 0, a3 & "-(" & ss1 & ")", _
- s2 = 5 And (s1 - 1) * (s1 - 2) * (s1 - 5) <> 0, a3 & "-" & ss1, _
- s2 = 6, a3 & "÷(" & ss1 & ")")
-
- ' 根据不同的情况,添加括号
- ss3 = Switch(s3 = 1, ss2 & "+" & a4, _
- s3 = 2, ss2 & "-" & a4, _
- s3 = 3 And (s2 - 1) * (s2 - 2) * (s2 - 5) = 0, "(" & ss2 & ")×" & a4, _
- s3 = 3 And (s2 - 1) * (s2 - 2) * (s2 - 5) <> 0, ss2 & "×" & a4, _
- s3 = 4 And (s2 - 1) * (s2 - 2) * (s2 - 5) = 0, "(" & ss2 & ")÷" & a4, _
- s3 = 4 And (s2 - 1) * (s2 - 2) * (s2 - 5) <> 0, ss2 & "÷" & a4, _
- s3 = 5 And (s2 - 1) * (s2 - 2) * (s2 - 5) = 0, a4 & "-(" & ss2 & ")", _
- s3 = 5 And (s2 - 1) * (s2 - 2) * (s2 - 5) <> 0, a4 & "-" & ss2, _
- s3 = 6, a4 & "÷(" & ss2 & ")")
-
- Sheets(1).[c4] = ss3 & "=24"
-
- Exit Sub
- End If
- 300
- Next
- 200
- Next
- 100
- Next
- Next
复制代码
(未完,接下一段) |
|