设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 9550|回复: 20
打印 上一主题 下一主题

[高1]求1*2*3*...*1000的值

[复制链接]
1#
发表于 2005-3-30 06:13:00 | 显示全部楼层


下面的程序效率不是很高,但超大整数乘法、加法的函数还是比较实用

Public Function factorial(num As Integer) As String

    factorial = "1"

    For num = num To 1 Step -1

        factorial = strMult(factorial, Trim(Str(num)))

    Next

End Function

'1000 的阶乘=40238726007709377354370243392300398571937...用时6分钟

'超大整数乘法 (需调用下面的strAdd、strPlus自定义函数)

Public Function strMult(str1 As String, str2 As String) As String

    Dim i As Integer

    If Len(str2) = 1 Then

        strMult = IIf(str2 = "0", "", str1)

        For i = 1 To Val(str2) - 1

            strMult = strAdd(strMult, str1)

        Next

    Else

        strMult = strAdd(strMult(str1, Right(str2, 1)), strMult(str1 & "0", Left(str2, Len(str2) - 1)))

    End If

End Function

'超大整数加法 (需调用下面的strPlus自定义函数)

Public Function strAdd(str1 As String, str2 As String) As String

    If Len(str1) < Len(str2) Then

        strAdd = strAdd(str2, str1)

    Else

        Dim num As Long

        strAdd = strPlus(str1, "", Val(Right(str2, 1)))

        For num = 1 To Len(str2) - 1

            strAdd = strPlus(Left(strAdd, Len(strAdd) - num), Right(strAdd, num), Val(Mid(str2, Len(str2) - num, 1)))

        Next

    End If

End Function

Public Function strPlus(str1 As String, str2 As String, num As Integer) As String

    If Len(str1) = 1 Then

        strPlus = (Val(str1) + num) & str2

    Else

        num = Val(Right(str1, 1)) + num

        If num < 10 Then

            strPlus = Left(str1, Len(str1) - 1) & num & str2

        Else

            strPlus = strPlus(Left(str1, Len(str1) - 1), (num Mod 10) & str2, 1)

        End If

    End If

End Function

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-13 17:13 , Processed in 0.081697 second(s), 24 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表