这里使用VBScript做一个示范吧:
Dim ax, ex
ax=10^10
ex=""
Function Rec (Str1, InsertAt, Plus_N)
'注释:Str1:当前字符串。InsertAt:当前指针的位置。Plus_N:剩余的加号数量
Dim a, StrA, StrB, StrC
If InsertAt = Len(Str1) Then '如果搜索到了字符串的末尾
If Plus_N = 0 Then '如果剩余加号数量为0,(而不是负数或正数)
a = Eval(Str1) '计算结果值 if a<ax Then '如果小于“最优值” ax = a '储存他
ex = Str1
End If
Else
Exit Function
End If
Else
StrA = Mid (Str1, 1, InsertAt)'把字符串分段
StrB = Mid (Str1, InsertAt + 1, Len(Str1) - InsertAt)
Rec StrA & "+" & StrB, InsertAt + 2, Plus_N - 1 '先尝试在其中插入加号的情况
Rec StrA & StrB, InsertAt + 1, Plus_N '再尝试不插入加号的情况。这样就能遍历所有的情况了。
End If
End Function