wzkl.net
当前位置:首页 >> vB编写求n!的递归函数FACt >>

vB编写求n!的递归函数FACt

Private Sub Command1_Click() n = CInt(InputBox("请要求几的阶乘?")) MsgBox (n & "!=" & fact(n)) End Sub Function fact(n) s = 1 For i = 2 To n s = s * i Next i fact = s End Function

Private Sub Command1_Click() n = CInt(InputBox("N=")) y = fac(n) MsgBox n & "!=" & y End Sub Function fac(n) If n = 0 Or n = 1 Then fac = 1 Else fac = n * fac(n - 1) End Function

非常简单: Function Factorial(ByVal n As Integer) As Long If n < 2 Then Factorial = 1 Else Factorial = n * f(n - 1) End IfEnd Function

Private Sub Form_Click() Print fact(4)End SubPublic Function fact(n As Integer) As Long If n = 1 Then fact = 1 Else fact = n * fact(n - 1) End If 'Print fact;End Function

Public Function s(n As Integer) As Long If n = 1 Then s = 1 Else s=n * s(n-1) End If End Function Private Sub Form_Click() Print “s(5)=”;s(5) End Sub

Function fun(n As Integer) As Long If n = 0 Or n = 1 Then fun = 1 Else fun = n * fun(n - 1) End IfEnd FunctionSub test() MsgBox fun(5)End Sub理解递归,不能从像一般函数那样用过程的方式来理解,而要用“功能”的方式去理解,如果你有面...

Form1上放一个文本框Text1, 一个按钮Command1。 在框中输入某个数字,例如10,代码会将列出f(1)至f(10)的值。 如果你只需要特定的第n项,比如第8项,就直接调用DiGui(8)即可。 代码: Private Sub Command1_Click() Dim n As Long n = CInt(Tex...

Private Sub Command1_Click() Dim n As Integer n = InputBox("请输入第几个月:") Print "Fib1("; n; ")="; fib1(n) Print "Fib2("; n; ")="; fib2(n)End SubFunction fib1(n As Integer) a = 1 b = 1 c = 1 For i = 3 To n c = a + b a = b ...

第1题: 添加一个按钮,完整代码如下: Option Explicit Private Sub Command1_Click() Print Y(20) End Sub Private Function Y(ByVal n As Integer) As Single If n = 1 Then Y = 2 / 1 Else Y = (n + 1) / n + Y(n - 1) End If End Function ...

Function c(ByVal n, ByVal m) If n > m / 2 Then n = m - n If n = 0 Then c = 1 ElseIf n = 1 Then c = m Else c = c(n, m - 1) + c(n - 1, m - 1) End IfEnd FunctionPrivate Sub Command1_Click() Dim n As Integer, m As Integer n = Val(...

网站首页 | 网站地图
All rights reserved Powered by www.wzkl.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com