VB上机等级考试常用算法.docx
《VB上机等级考试常用算法.docx》由会员分享,可在线阅读,更多相关《VB上机等级考试常用算法.docx(13页珍藏版)》请在冰豆网上搜索。
![VB上机等级考试常用算法.docx](https://file1.bdocx.com/fileroot1/2022-11/16/6095cbd6-37a3-4008-b6f0-c1a43d5220c0/6095cbd6-37a3-4008-b6f0-c1a43d5220c01.gif)
VB上机等级考试常用算法
VB等级考试常用算法
1.求因子数
OptionExplicit
PrivateSubCommand1_Click()
DimaAsInteger,b()AsInteger
a=InputBox("请输入一个正整数")
Callfactor(a,b)
Fora=1ToUBound(b)
Printb(a);
Next
EndSub
PrivateSubfactor(xAsInteger,fn()AsInteger)
DimiAsInteger,nAsInteger
ReDimfn
(1)
n=1
fn
(1)=1
Fori=2Tox/2
IfxModi=0Then
n=n+1
ReDimPreservefn(n)
fn(n)=i
EndIf
Nexti
EndSub
2.求因子数之和
OptionExplicit
PrivateSubCommand1_Click()
DimaAsInteger,bAsInteger
a=InputBox("请输入一个正整数")
b=sum_factor(a)
Print"因子之和为:
"
Printb;
EndSub
PrivateFunctionsum_factor(xAsInteger)AsLong
DimiAsInteger,sAsInteger
s=1
Print1;
Fori=2Tox/2
IfxModi=0Then
s=s+i
Printi;
EndIf
Nexti
'DoWhilei'i=i+1
'IfnModi=0Then
's=s+i
'EndIf
'Loop
Print
sum_factor=s
EndFunction
3.判断数是否为素数
OptionExplicit
PrivateSubCommand1_Click()
DimaAsInteger
a=InputBox("请输入一个正整数")
Ifprime(a)ThenPrinta;"是素数!
"ElsePrinta;"不是素数!
"
EndSub
PrivateFunctionprime(xAsInteger)AsBoolean
DimiAsInteger
'Fori=2Tox-1
'Fori=2Tox/2
Fori=2ToSqr(x)
IfxModi=0Then
ExitFunction
EndIf
Nexti
prime=True
EndFunction
4.求最大公约数
OptionExplicit
PrivateSubCommand1_Click()
DimaAsInteger,bAsInteger
a=InputBox("请输入一个正整数a:
")
b=InputBox("请输入一个正整数b:
")
Printa&"和"&b&"最大公约数是:
"&gcd(a,b)
EndSub
PrivateFunctiongcd(xAsInteger,yAsInteger)AsInteger
IfxMody=0Then
gcd=y
Else
gcd=gcd(y,xMody)
EndIf
EndFunction
5.求最小公倍数
OptionExplicit
PrivateSubCommand1_Click()
DimaAsInteger,bAsInteger
a=InputBox("请输入一个正整数a:
")
b=InputBox("请输入一个正整数b:
")
Printa&"和"&b&"最小公倍数是:
"&lcm(a,b)
EndSub
PrivateFunctionlcm(xAsInteger,yAsInteger)AsLong
DimxyAsLong
xy=x*y
Do
IfxyModx=0AndxyMody=0Thenlcm=xy
xy=xy-1
LoopWhilexy>xAndxy>y
EndFunction
PrivateFunctionlcm1(xAsInteger,yAsInteger)AsLong
Do
lcm=lcm+x
IflcmModx=0AndlcmMody=0ThenExitDo
Loop
EndFunction
6.求阶乘
OptionExplicit
PrivateSubCommand1_Click()
DimnAsInteger,bAsInteger
n=InputBox("请输入一个正整数n:
")
Printn&"!
=";fact(n)
EndSub
PrivateFunctionfact(xAsInteger)AsLong
Ifx<=1Then
fact=1
Else
fact=x*fact(x-1)
EndIf
EndFunction
PrivateFunctionfact1(xAsInteger)AsLong
DimiAsInteger
fact1=1
Fori=2Tox
fact1=fact1*i
Next
EndFunction
7.求分数约数
OptionExplicit
PrivateSubCommand1_Click()
DimaAsInteger,bAsInteger
a=InputBox("请输入一个正整数a:
")
b=InputBox("请输入一个正整数b:
")
Print"分数"&a&"/"&b&"可以约分为:
";
divisora,b
Printa&"/"&b
EndSub
PrivateSubdivisor(fzAsInteger,fmAsInteger)
DimminAsInteger,iAsInteger
IffzFori=2Tomin
DoWhilefzModi=0AndfmModi=0
fz=fz/i
fm=fm/i
Loop
Next
EndSub
8.一维数组排序
OptionExplicit
OptionBase1
PrivateSubCommand1_Click()
DimiAsInteger,a()AsInteger,nAsInteger
n=InputBox("请输入数组元素的个数n:
")
ReDima(n)
Randomize
Fori=1Ton
a(i)=Int(Rnd*100)+1
Printa(i);
Next
Print
bub_sorta'可以使用不同的方法
Fori=1Ton
Printa(i);
Next
Print
EndSub
PrivateSubchs_sort(a()AsInteger)
DimiAsInteger,jAsInteger,tAsInteger,kAsInteger
Fori=1ToUBound(a)-1
k=i
Forj=i+1ToUBound(a)
Ifa(i)>a(j)Then
t=a(i):
a(i)=a(j):
a(j)=t
EndIf
Next
Next
EndSub
PrivateSubbub_sort(a()AsInteger)
DimiAsInteger,jAsInteger,tAsInteger,kAsInteger
Fori=1ToUBound(a)-1
Forj=1ToUBound(a)-i
Ifa(j)>a(j+1)Then
t=a(j):
a(j)=a(j+1):
a(j+1)=t
EndIf
Next
Next
EndSub
9.判断一对数是否为互质数
OptionExplicit
PrivateSubCommand1_Click()
DimaAsInteger,bAsInteger
a=InputBox("请输入第1个正整数")
b=InputBox("请输入第2个正整数")
Printa&"和"&b&"是:
";
Ifmutual_prime(a,b)ThenPrint"互质数!
"ElsePrint"不是互质数!
"
EndSub
PrivateFunctionmutual_prime(xAsInteger,yAsInteger)AsBoolean
DimiAsInteger,minAsInteger
Ifx'Fori=2ToSqr(min)
'Fori=2Tomin/2
Fori=2Tomin-1
IfxModi=0AndyModi=0Then
ExitFunction
EndIf
Nexti
mutual_prime=True
EndFunction
10.判断数是否为降序
OptionExplicit
OptionBase1
PrivateSubCommand1_Click()
DimiAsInteger,a()AsInteger,nAsInteger
n=InputBox("请输入一个任意正整数:
")
Printn
Iforder1(n)ThenPrint"是一个降序!
"ElsePrint"不是一个降序!
"
EndSub
PrivateFunctionorder(sAsInteger)AsBoolean
DimiAsInteger,nAsInteger,a()AsInteger
n=Len(CStr(s))
Fori=1Ton
ReDimPreservea(i)
a(i)=sMod10
s=s\10
Next
Fori=1Ton-1
Ifa(i)>a(i+1)ThenE