[ VB2010 ] - 遞迴程式 --計算N!值
Example1 計算N!值
Example2 計算M取N的值
Example3 計算1-N的和
Example4 計算(N-1)N的值
Example5 求最大公因數
Example6 輸入兩數字A, B,利用遞迴求得A的B次方
Example7 二元搜尋法 (binary search)
Example8 陣列元素總和
Public Class Form1
Private Function factorial(ByVal n As Integer) '/*遞迴程式部分*/
If n = 1 Then
Return 1
Else
Return n * factorial(n - 1)
End If
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim n As Integer
n = Val(TextBox2.Text)
TextBox3.Text = factorial(n).ToString()
End Sub
End Class
沒有留言:
張貼留言