当前位置: 代码迷 >> Lotus >> 递归算法在这个例子如何用
  详细解决方案

递归算法在这个例子如何用

热度:134   发布时间:2016-05-05 06:50:30.0
递归算法在这个例子怎么用?
现在有两个数组:
dim a as Variant,b as variant
a   和    b     数据内容如下  
1             2
2             6
3             8
5             7
2             9

怎么用递归算法求出 function tempstring(s as string) as string
当a(i)=1时,就会b(i)=2, 
再把a(i)=2,找对应的b(i)
最后返回 2:6:9

当tempstring(1)时,返回 2:6:9

这个递归不能求出来,在哪里错?
function tempstring(s as string) as string
    dim s1 as string
   s1=""
      For i=0 To Ubound(a)
           If v(i)=sss Then
        If s1="" Then
           s1=b(i)
        Else
           s1=s1 & ":" & b(i) 
        End If
        tempstring=s1 & ":" & tempstring(b(i))
           End If 
       Next 
End Function
------解决思路----------------------
我在机器上运行过成功的:

'这两个变量定义到全局变量区
Dim a As Variant
Dim b As Variant

Sub Click(Source As Button)
a=Evaluate({"1":"2":"3":"5":"2"})
b=Evaluate({"2":"6":"8":"7":"9"})

Msgbox tempstring("1") '返回的结果多个":",自己修改吧

End Sub

Function tempstring(s As String) As String
Dim s1 As String
s1=""
For i=0 To Ubound(a)
If a(i)=s Then
If s1="" Then
s1=b(i)
Else
s1=s1 & ":" & b(i) 
End If
tempstring=s1 & ":" & tempstring(b(i))
End If 
Next 
End Function 
  相关解决方案