资源描述:
《vb版约瑟夫算法》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、vb版约瑟夫算法题目就是:n个人围成一圈报数,数到m的人出列,下一次从m的下一个人开始报数再到m后出列,直到最后一个人也出列,并且打印出每次出列的人是谁ben1222...又到处发帖...那我也再帖一次...看看这个算法是否更好集合版:PrivateSubCommand1_Click() DimaAsNewCollection DimnAsInteger,mAsInteger m=Val(Text2.Text) Forn=1ToVal(Text1.Text) a.AddCStr(n) Next n=0 While
2、a.Count>0 n=((m+n-1)Moda.Count)+1 Debug.Printa.Item(n) a.Removen n=n-1 WendEndSub书林跋涉:对象链表版我记得大学时候用约瑟夫环算法求过全排列今天有空改成vb的了大家帮忙测试一下速度吧:)clsnode.cls如下PublicPropertyLetdata(ByValvDataAsLong)m_Value=vDataEndPropertyPublicPropertyGetdata()AsLongdata=m_Value
3、EndPropertyPublicPropertyLetnextnode(ByValvDataAsClsNode)Setnode=vDataEndPropertyPublicPropertyGetnextnode()AsClsNodeSetnextnode=nodeEndProperty主要代码如下:PrivateListNode()AsClsNodePrivateSubCommand1_Click()DimkAsLong DimsT! sT=Timerk=Val(Text1.Text)CallAllRank(k)MsgBox"总用
4、时:"&Format(Timer-sT,"0.000")&"s"EndSubPrivateSubAllRank(ByValnAsLong)DimiAsLong,jAsLong,kAsLong,lAsLongIfn>12Orn<1ThenMsgBox"太大了":ExitSubReDimListNode(1Ton)AsClsNodeFori=1Ton SetListNode(i)=NewClsNode ListNode(i).data=iNext''''''''''''''''''''''''''l=factorial(n-1)Fori
5、=1Ton Forj=1Tol Josephusn,i,j NextNext''''''''''''''''''''''''''''Fori=1Ton SetListNode(i)=NothingNextEndSubPrivateFunctionfactorial(ByValnAsLong)AsLongDimsumAsLong,iAsLongsum=1Fori=1Tonsum=sum*iNextfactorial=sumEndFunctionPrivateSubJosephus(ByValnAsLong,ByValstartAs
6、Long,ByValmAsLong)DimiAsLong,PriorAsLongDimpAsClsNode,ptrAsClsNodeDimcountAsLong,tAsLong'建立循环链表Fori=1Ton-1 ListNode(i).nextnode=ListNode(i+1)Next ListNode(n).nextnode=ListNode(1)'总结点数 count=n'找开始报数的对应结点 Setp=ListNode(start)'找当前结点的直接前趋结点Ifstart>1Then Prior=start-1Els
7、e Prior=nEndIf Setptr=ListNode(Prior) 'Debug.PrintvbCrLfDoWhilecount>0 t=(m-1)Modcount Fori=1Tot Setptr=p Setp=p.nextnode Next 'Debug.Printp.data, 'Me.Printp.data, ptr.nextnode=p.nextnode count=count-1 Setp=ptr.nextnodeLoop'Debug.Print'Me.PrintEndSub
8、bz3zwy:数组链表版OptionExplicitPrivateSubCommand1_Click()Constm%=8Dima%(1Tom)Dimi%,j%,n%,p%Fori=1Tom-1:a(i)=