转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
一道面试题, 求 1000!的位数(VBS实现)         ★★★★

一道面试题, 求 1000!的位数(VBS实现)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1479 更新时间:2009/4/23 15:04:02
http://community.csdn.net//Expert/TopicView2.asp?id=4498174&datebasetype=now

Option Explicit
''''VBS 实现求 1000! 位数
''''作者: xiaoyuehen(萧月痕)
''''日期: 2006-1-6
''''MSN: xiaoyuehen(at)msn.com

''''大数加法
''''参数 x: 加数, y: 被加数
''''以数组存储
function GetAddResult(x, y)
 Dim arrR(), strX, strY, lx, ly
 strX = StrReverse(x)
 strY = StrReverse(y)
 lx = Len(strX)
 ly = Len(strY)
 ''''Msgbox lx

 Dim i, iMax, iJinWei
 If lx > ly Then
  iMax = lx
 Else
  iMax = ly
 End If

 ReDim arrR(iMax)
 iJinWei = 0

 For i = 1 to iMax
  arrR(i) = int("0" & Mid(strX, i, 1)) + int("0" & Mid(strY, i, 1)) + iJinWei

  iJinWei = arrR(i) \ 10
  arrR(i) = Right(arrR(i), 1)
 Next

 Dim strResult
 strResult = StrReverse(Join(arrR, ""))

 If Left(strResult, 1)  = "0" Or iJinWei = 1 Then
  strResult = "1" & strResult
 End If
 GetAddResult = strResult
End function

''''加法
''''参数 x: 加数, y: 被加数
''''以字符串构造
''''该方法经过试验不可行..速度太慢!
function GetAddNResult(x, y)
 Dim strX, strY, lx, ly
 strX = StrReverse(x)
 strY = StrReverse(y)
 lx = Len(strX)
 ly = Len(strY)

 Dim i, iMax, iJinWei
 If lx > ly Then
  iMax = lx
 Else
  iMax = ly
 End If

 Dim iTemp, strResult
 iJinWei = 0
 iTemp = 0

 For i = 1 to iMax
  iTemp = int("0" & Mid(strX, i, 1)) + int("0" & Mid(strY, i, 1)) + iJinWei

  iJinWei = iTemp \ 10
  strResult = (iTemp Mod 10) & strResult
 Next

 If Left(strResult, 1)  = "0" Or iJinWei = 1 Then
  strResult = "1" & strResult
 End If
 GetAddNResult = strResult
End function


''''乘法
''''参数 x: 大数, y: 小数(一位)
function GetMulResult(x, y)
 GetMulResult = 0
 Dim i
 For i = 1 to y
  GetMulResult = GetAddResult(GetMulResult, x)
 Next
End function

''''乘法
''''参数 x: 大数, y: 小数
function GetMultResult(x, y)
 GetMultResult = 0

 Dim str, lx, ly, i
 ly = Len(y)
 For i = 1 to ly
  GetMultResult = GetAddResult(GetMultResult, GetMulResult(x, CInt(Mid(y, i, 1))) & String(ly - i, "0"))
 Next
End function

''''乘法
''''参数 x: 大数, y: 大数
function GetMulcResult(x, y)
 If Len(x) > Len(y) Then
  GetMulcResult = GetMultResult(x, y)
 Else
  GetMulcResult = GetMultResult(y, x)
 End If
End function

''''阶乘
''''参数 x: 自然数
function GetFactorialResult(x)
 GetFactorialResult = x
 Dim i
 For i = (x - 1) to 2 step - 1
  GetFactorialResult = GetMulcResult(GetFactorialResult, i)
 Next
End function

''''直接求阶乘位数
''''参数 x: 自然数
''''原作: aiur2000(闭关失败,走火入魔,开关拉!)
''''vbs代码实现: xiaoyuehen
function GetFactorialLength(x)
 Dim iLen, iSum, iVal, i

 ''''思路:因为不计算具体值,所以没必要全部算出,只用计算能影响到结果的位数即可,同时要加入下次影响结果的值.
 ''''做法:每次的积取下次乘数的位数的2倍即可.

 ''''下一个乘数的位数
 iLen = 0

 ''''位数
 iSum = 0

 ''''积
 iVal = 1
 
 For i = 1 to x
  iLen = Len(i)

  ''''如果积超过乘数位数2倍,后面的记入汇总位数
  If Len(iVal) > iLen * 2 Then
   iSum = iSum + (Len(iVal) - iLen * 2)
   iVal = Left(iVal, iLen * 2)
  End If

  iVal = iVal * i
 Next
 
 GetFactorialLength = Len(iVal) + iSum
End function

''''下面开始各项测试

Dim t, x, y
t = Timer
x = String(1000, "9")
y = String(1000, "9")
''''y = String(200, "9")
''''Msgbox " String 总用时: " & FormatNumber(Timer - t) & " 秒"


''''t = Timer
''''Call GetAddResult(x, y)
''''Msgbox " GetAddResult: " & "GetAddResult(x, y)" & " 总用时: " & FormatNumber(Timer - t) & " 秒"

t = Timer
''''Call GetFactorialLength(1000)
Msgbox " GetFactorialLength(1000): " & GetFactorialLength(1000) & " 总用时: " & FormatNumber(Timer - t) & " 秒"

''''t = Timer
''''Call GetMulResult(x, y)
''''Msgbox " GetMulResult: " & GetMulResult(x, y) & " 总用时: " & FormatNumber(Timer - t) & " 秒"

''''t = Timer
''''Call GetMulResult(x, y)
''''Msgbox " GetMultResult: " & GetMultResult(x, y) & " 总用时: " & FormatNumber(Timer - t) & " 秒"

''''t = Timer
''''Call GetMulResult(x, y)
''''Msgbox " GetMulcResult: " & GetMulcResult(x, y) & " 总用时: " & FormatNumber(Timer - t) & " 秒"

''''t = Timer
''''Call GetAddResult(x, y)
''''Msgbox " GetAddResult 总用时: " & FormatNumber(Timer - t) & " 秒"

''''t = Timer
''''Call GetAddNResult(x, y)
''''Msgbox " GetAddNResult 总用时: " & FormatNumber(Timer - t) & " 秒"

''''x = 300
''''t = Timer
''''Call GetFactorialResult(x)
''''Msgbox " GetFactorialResult(" & x & ") = " & GetFactorialResult(x) & " 总用时: " & FormatNumber(Timer - t) & " 秒"
''''Msgbox " 共 " & Len(GetFactorialResult(x)) & " 位, 用时: " & FormatNumber(Timer - t) & " 秒"


[VB.NET程序]改进后的mkw3site.vbs(创建虚拟目录)  [VB.NET程序]把VB的程序写成VBScript方式放在VBS文件中
[VB.NET程序]世界第一等-----无须安装WSH而执行VBS  [VB.NET程序]蛙蛙推荐:VBS的数据库操作类,
[VB.NET程序]实现E-mail地址验证的vbs函数代码  [VB.NET程序]VBS 中 Space 函数的应用
[VB.NET程序]目录浏览,目录在左,文件在右(VBS)  [VB.NET程序]一句话木马用upfile.vbs
[VB.NET程序]如何修复:Windows上面的WScript的脚本(.vbs)不能…  [Sql Server]一个用Wsh来控制SqlServer的Dcom的VBs
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台