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

非常简单: 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 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

private function fac(n as integer) as integer dim i,sum as integer sum=1 for i=1 to n sum=sum*i next fac=sum end function 亦可用递归: private function fac(n as integer) as integer if n=1 then fac=1 else fac=fac(n-1)*n endif en...

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 ...

窗体中放入Text控件和命令按钮.取text中的数字作为N代码如下Private Function F(n As Long) As Long If n > 2 Then F = F(n - 1) + F(n - 2) Else F = 1 End If End FunctionPrivate Sub Command1_Click() Cls Dim I As Long I = Val(Text1.Text...

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

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理解递归,不能从像一般函数那样用过程的方式来理解,而要用“功能”的方式去理解,如果你有面...

private function dg(byval n as long) as long if n>1 then dg=n*(n-1) dg=dg+dg(n-2) end if end function 以上是阶乘的递归函数用键盘输入就用inputbox private sub command1_click() dim cnm as long,n as long,m as long n=val(inputbox("…...

m = Val(Text1) val函数返回的数据类型是双精度(Double)型,而你的gcd函数定义里用了m%,表示m是整型(Integer),所有会出现这种错误. 可以改成:m% = Val(Text1): n% = Val(Text2) 或者将gcd函数的参数以传值Byval方式(默认是ByRef传引用):Function ...

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