# 这种问题不能光用回圈

Posted by Honey on November 16, 1998 at 18:59:48:

In Reply to: Re: 有谁会作100阶乘 posted by Windows NT user on November 16, 1998 at 17:50:38:

这种问题不能光用回圈
否则大概到14!就溢位了
提供你以下程式给你参考
在表单上放 Command1 Text1
将Text1属性MultLin设成True
属性ScrollBars设成3

Option Explicit

Private Sub Command1_Click()
Dim LngH() As Long
Dim IntJ As Integer, IntK As Integer, IntI As Integer
Dim IntU As Integer
Text1.Text = ""
IntK = 1 '/*初设阵列只有一个*/
IntU = 1 '/*阵列不为零的使起数, 由于 _
经阶乘,后面会有多个零,遍不须列入计算 _
以节省时间*/
ReDim LngH(1 To IntK)
LngH(1) = 1
For IntJ = 2 To 100
'/*100!*/
For IntI = IntK To IntU Step -1
LngH(IntI) = LngH(IntI) * IntJ
If LngH(IntI) > 100000 Then
If UBound(LngH) <= IntI Then
ReDim Preserve LngH(1 To IntI + 1)
IntK = IntK + 1
End If
LngH(IntI + 1) = LngH(IntI + 1) + LngH(IntI) \ 100000
LngH(IntI) = LngH(IntI) Mod 100000
End If
Next
If LngH(IntU) = 0 Then
IntU = IntU + 1
End If
Next

Text1.Text = LTrim$(RTrim$(Str$(LngH(IntK))))

For IntI = UBound(LngH) - 1 To 1 Step -1
Text1.Text = Text1.Text & Chr(13) + Chr(10) + Format(LTrim$(RTrim$(Str$(LngH(IntI)))), "00000")
Next
End Sub

Follow Ups: