欢迎来到天天文库
浏览记录
ID:27514576
大小:112.00 KB
页数:15页
时间:2018-12-03
《vb典型算法归纳》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、VB典型算法归纳2.2.1求最大公约数15PrivateSubCommand1_Click()DimmAsInteger,nAsIntegerDimrAsIntegerm=Text1.Textn=Text2.TextDor=mModnm=nn=rLoopWhiler<>0Text3.Text=mEndSub PrivateSubCommand2_Click()EndEndSub PrivateSubCommand3_Click()Text1=""Text2=""Text3=""EndSub15152.2.2几种不同的排序法Dims
2、(10)AsInteger,nAsIntegerPrivateSubCommand1_Click()Picture1.ClsRandomizen=10Fori=1To10s(i)=Int(100*Rnd)+1Picture1.Prints(i);NextiPicture1.PrintPicture1.PrintEndSub‘选择排序法PrivateSubCommand2_Click()Fori=1Ton-1'外层循环N-1次Forj=i+1Ton'内层依赖外层Ifs(j)>s(i)Thent=s(i)'交换s(i)=s(j)s(j
3、)=tEndIfNextjNextiFori=1TonPicture1.Prints(i);NextiPicture1.Print15Picture1.PrintEndSub‘直接排序法PrivateSubCommand3_Click()Fori=1Ton-1pointer=i'初始化pointer,在每轮比较开始处Forj=i+1TonIfs(j)>s(pointer)Thenpointer=jNextjIfi<>pointerThent=s(i)s(i)=s(pointer)s(pointer)=tEndIfNextiFori
4、=1TonPicture1.Prints(i);NextiPicture1.PrintPicture1.PrintEndSub’冒泡排序法PrivateSubCommand4_Click()Fori=1Ton-1Forj=1Ton-i'比较次数逐次减少Ifs(j)
5、找方法OptionBase1Dims(10)AsInteger,xAsInteger,nAsIntegerPrivateSubCommand1_Click()DimiAsIntegern=10Fori=1Tons(i)=Int(100*Rnd)+1Picture1.Prints(i);NextiPicture1.Printx=InputBox("输入待查找的数:")EndSub PrivateSubCommand2_Click()‘顺序查找Fori=1ToUBound(s)Ifs(i)=xThenExitForNexti'退出的两
6、种情况Ifi<=UBound(s)ThenPicture1.Print"找到"&x&"它的位置数为:"&IElsePicture1.Print"对不起,没找到!"EndIfEndSub PrivateSubCommand3_Click()‘二分查找15DimresultAsBooleanDimtopAsInteger,bottomAsInteger,middleAsIntegerFori=LBound(s)ToUBound(s)–1‘先排序,后查找Forj=i+1TonIfs(i)>s(j)Thentemp=s(i)s(i)=s(
7、j)s(j)=tempEndIfNextjNextiPicture1.Print"排序后的数组是:";Fori=LBound(s)ToUBound(s)Picture1.Prints(i);NextiPicture1.Printresult=False'初始化逻辑变量top=LBound(s)'初始化指针bottom=UBound(s)DoWhile(top<=bottom)middle=(bottom+top)/2Ifx=s(middle)Thenresult=True'找到ExitDoElseIfx>s(middle)Then
8、'未找到,根据大小确定下一步比较范围top=middle+1Elsebottom=middle-1EndIfLoopIfresultThenPicture1.Print"找到"&x&"它的位置数为:"&CInt(middle)ElsePicture
此文档下载收益归作者所有