用VB求1到10的阶乘 递归算法 越简单越好
收起
知与谁同
2018-07-17 17:58:38
7934
0
6
条回答
写回答
取消
提交回答
-
Function A(n As Long) As Double
If n > 0 Then A = IIf(n = 1, 1, n * A(n - 1))
End Function
举例说明:使用方法为 Msgbox a(12)
程序就会求出12的阶乘,其结果为479001600
最大可以计算到 170的阶乘
2019-07-17 22:55:31
-
Private Sub Command1_Click()
Print f(10)
End Sub
Function f(n As Integer) As Long
If n = 0 Or n = 1 Then
f = 1
Else
f = n * f(n - 1)
End If
End Function
2019-07-17 22:55:31
-
function jc(byval n as integer)
if n=1 then
return 1
else
return jc(n-1)*n
end if
end function
2019-07-17 22:55:31
-
Private Sub Command1_Click()
Print Jie(5)
End Sub
Private Function Jie(ByVal i As Integer)
If i = 1 Then
Jie = 1
Exit Function
End If
Jie = i * Jie(i - 1)
End Function
2019-07-17 22:55:30
-
Dim s As Long
s = 1
For i = 1 To 10 Step 1
s = s * i
Next i
Print s
已经验证过了没问题~~~
LZ 加油啊~
2019-07-17 22:55:30
-
VB 递归算法 求阶乘
Function Factorial(n As Long) As Long
If n = 1 Then Factorial = 1 Else Factorial = n * Factorial(n - 1)
End Function
Private Sub Command1_Click()
Dim n As Long
For n = 1 To 10
Print CStr(n); "!="; Factorial(n)
Next n
End Sub
2019-07-17 22:55:30
滑动查看更多