首页 > 代码库 > 递归的感悟

递归的感悟

 求n阶乘的VB函数过程。  
Private Function F( byval n As Integer ) As long  
    If n=1 Then  
        F=1  
    Else:  
        F=n*F(n-1)  
    End If  
End Function  
让我们来跟踪这个程序的计算过程,令n=4调用这个函数,用下面的形式来表示递归求解的过程:  
(1)F(4)=4×F(3)      n=4调用函数过程F(3)  2)F(3)=3×F(2)      n=3调用函数过程F(2)  3)F(2)=2×F(1)      n=2调用函数过程F(1)  4)F(1)=1             n=1求得F(1)的值  5)F(2)=2×1=2       回归,n=2,求得F(2)的值  6)F(3)=3×2=6       回归,n=3,求得F(3)的值  7)F(4)=4×6=24      回归,n=4,求得F(4)的值求
 
Private Function Fibo(ByVal n As Integer) As Integer  
                    函数的返回值为第n个斐波那契数  
    If (n=0) Then  
        Fibo=0  
    ElseIf n=1 Then  
        Fibo=1  
    Else: Fibo=Fibo(n-1)+Fibo(n-2)  
    End If  
End Function  
  
Dim q, le, ss  
Sub 排列组合()  
On Error Resume Next  
ss = ""  
strA = "运行下看"  
le = Len(strA)  
    处理 strA, le  
MsgBox ss  
End Sub  
Function 处理(strA, le)  
    For i = 1 To Len(strA)  
        a = Mid(strA, i, 1)  
        sy = Left(strA, i - 1) & Right(strA, Len(strA) - i)  
        q = q & a  
     If Len(q) = le Then  
     ss = ss & q & Chr(13)  
     End If  
        处理 sy, le  
    Next  
q = Left(q, Len(q) - 1)  
End Function  
  
  
Sub sdf()  
    a = "中文论坛"  
    l = Len(a)  
    For i = 1 To 4  
        For j = 1 To 4
Function F(n)  
    If n = 1 Then  
        F = 1  
        Else:  
        F = n * F(n - 1)  
    End If  
      
End Function  
Function e(n)  
    If n = 1 Then  
        e = 1  
    Else  
        e = e(n - 1) + F(n)  
    End If  
End Function
Sub sdf()  
    a = "中文论坛"  
    l = Len(a)  
    For i = 1 To 4  
        For j = 1 To 4  
            For k = 1 To 4  
                For l = 1 To 4  
                    If Mid(a, i, 1) <> Mid(a, j, 1) And Mid(a, i, 1) <> Mid(a, k, 1) And Mid(a, i, 1) <> Mid(a, l, 1) And Mid(a, j, 1) <> Mid(a, k, 1) _  
                    And Mid(a, j, 1) <> Mid(a, l, 1) And Mid(a, k, 1) <> Mid(a, l, 1) Then  
                    Debug.Print Mid(a, i, 1) & Mid(a, j, 1) & Mid(a, k, 1) & Mid(a, l, 1)  
                    End If  
                Next  
            Next  
        Next  
    Next  
  
  
End Sub  
双重递归  
Sub 计算阶乘和() 1!+2!+3!+…+n! ;使用双重递归完成  
    Debug.Print e(5)  
End Sub  
  
  
  
  
word表格转为分数:  
Dim s  
Sub sdf()  
s = ""  
Dim ta As Table, ro As Row  
Dim ta1 As Table  
    Set ta = Me.Tables(1)  
    处理 ta, s  
    s = "eq " + Replace(s, Chr(13), "")  
    ta.Delete  
Selection.InsertAfter s  
Application.Run "insertfieldchars"  
Selection.Fields.Update  
End Sub  
Function 处理(ta As Table, s)  
    For i = ta.Rows.Count To 1 Step -1  
        If ta.Rows(i).Cells(1).Tables.Count > 0 Then  
            For j = 1 To ta.Rows(i).Cells(1).Tables.Count  
                处理内表 ta.Rows(i).Cells(1).Tables(1)  
            Next  
            i = i + 1  
        Else  
                m = Replace(ta.Rows(i).Cells(1).Range.Text, Chr(13) & Chr(7), "")  
                If s = "" Then  
                    s = m  
                Else  
                    s = "\f(" + m + "," + s + ")"  
                End If  
            End If  
        Next  
    End Function  
Function 处理内表(ta As Table)  
    处理 ta, s  
    ta.Select  
    ta.Delete  
    Selection.TypeText s  
    s = ""  
End Function  
    
Excel 如何用递归将嵌套表中数字换成分数形式?-Word-ExcelHome技术论坛 -  http://club.excelhome.net/forum.php?mod=viewthread&tid=1177443  

 

递归的感悟