在下面算式中,添上两个括号,圈掉一个运算符号,使等式成立.9*8+7*6-5*4+3*2+1=2003
在下面算式中,添上两个括号,圈掉一个运算符号,使等式成立.
9*8+7*6-5*4+3*2+1=2003
Private Sub Command1_Click()
Dim wz%(20)
Text2 = ""
t = Text1.Text
t = Replace(t, " ", "") & " "
u = t
t = Replace(t, "+", " ")
t = Replace(t, "-", " ")
t = Replace(t, "*", " ")
t = Replace(t, "/", " ")
j = 0: wz(j) = 0
For i = 2 To Len(t)
If Mid(t, i, 1) = " " Then j = j + 1: wz(j) = i
Next i
For i = 0 To j - 2
For k = i + 2 To j
y = ScriptControl1.Eval(Mid(u, wz(i) + 1, wz(k) - wz(i) - 1))
If y Label1.Caption = Left(u, wz(i)) & y & Mid(u, wz(k), wz(j) - wz(k))
Call wkh
Next k
Next i
End Sub
Private Sub wkh()
Dim wz%(20)
Text2 = ""
t = Label1.Caption
t = Replace(t, " ", "") & " "
u = t
t = Replace(t, "+", " ")
t = Replace(t, "-", " ")
t = Replace(t, "*", " ")
t = Replace(t, "/", " ")
j = 0: wz(j) = 0
For i = 2 To Len(t)
If Mid(t, i, 1) = " " Then j = j + 1: wz(j) = i
Next i
u = Replace(u, "|", "-")
For i = 0 To j - 2
For k = i + 2 To j
u1 = Left(u, wz(i)) & "(" & Mid(u, wz(i) + 1, wz(k) - wz(i) - 1) & ")" & Mid(u, wz(k), wz(j) - wz(k))
y = ScriptControl1.Eval(u1)
If Abs(y - 2003) Text2 = Text2 & u1 & vbCrLf
Next k
Next i
End Sub
Private Sub Form_Load()
ScriptControl1.Language = "VBScript"
End Sub
经过上面的程序逐个删除一个符号,运算得到2003的唯一解。
【9*(8+7*6)-5】*4+3*21百度地图
(9*8+76-5)*(4+3)*2+1=2003