要求:随意给出4个整数,通过 + - * / 运算,使答案等于24。例如:1*2*3*4=24,5+6*3+1=24 我自己编写了一个,有很多嵌套循环,谁有更好的思路,请写出来,大家一起研究 以下是我的程序代码:(自己优化了一下程序,可以快速运行了,重新粘贴代码,但是括号“()”的问题还有待解决) Option Explicit Sub a24() Dim x(3) As String, y(3) As Long Dim a As String, b As Long Dim i As Long, j As Long Dim m1 As Long, n As Long, n1 As Long, o As Long, o1 As Long, p As Long, p1 As Long Application.ScreenUpdating = False For i = 0 To 3 x(i) = Choose(i + 1, "+", "-", "*", "/") y(i) = InputBox("请输入第" & i + 1 & "个值", "x" & i + 1) Next i For m1 = 0 To 3 For n = 0 To 3 For n1 = 0 To 3 If n1 = m1 Then Exit For For o = 0 To 3 For o1 = 0 To 3 If o1 = m1 Or o1 = n1 Then Exit For For p = 0 To 3 For p1 = 0 To 3 If p1 = m1 Or p1 = n1 Or p1 = o1 Then Exit For a = y(m1) & x(n) & y(n1) & x(o) & y(o1) & x(p) & y(p1) b = Evaluate(a) If b = 24 Then MsgBox a & "=24" End If Next p1 Next p Next o1 Next o Next n1 Next n Next m1 MsgBox "不等于二十四" Application.ScreenUpdating = True End Sub
[此贴子已经被作者于2008-2-25 17:37:09编辑过] |