ave=ave+0.1*a(i)
Nexti
Text2=Text2&max&""
Text3=Text3&min&""
Text4=Text4&ave&""
EndSub
1.百元百鸡
PrivateSubCommand1_Click()
DimxAsInteger,yAsInteger,zAsInteger
List1.AddItem"公鸡母鸡小鸡"
Forx=1To20
Fory=1To33
z=100-x-y
If5*x+3*y+z/3=100Then
List1.AddItemCStr(x)&Space(6)&CStr(y)&Space(6)&CStr(z)
EndIf
Next
Next
EndSub
PrivateSubCommand2_Click()
List1.Clear
EndSub
PrivateSubCommand3_Click()
End
EndSub
2.递归函数求n阶乘
PrivateSubCommand1_Click()
List1.Clear
DimiAsInteger
Fori=1To7
List1.AddItemfact(i)
Nexti
EndSub
Functionfact(ByValnAsDouble)AsDouble
Ifn=1Then
fact=1
Else
fact=n*fact(n-1)
EndIf
EndFunction
PrivateSubCommand2_Click()
List1.Clear
EndSub
PrivateSubCommand3_Click()
End
EndSub
3.回文数
PrivateSubCommand1_Click()
Randomize
DimiAsInteger,xAsInteger,minAsInteger
Fori=1To500
x=Int(Rnd*9000)+1000
Text1=Text1&x&vbCrLf
IfHuiWenShu(x)Then
Text2=Text2&x&vbCrLf
Ifmin=0Then
min=x
Else
Ifmin>xThen
min=x
EndIf
EndIf
EndIf
Nexti
Text3=min
EndSub
FunctionHuiWenShu(ByValaAsInteger)AsBoolean
IfStrReverse(a)=aThen
HuiWenShu=True
Else
HuiWenShu=False
EndIf
EndFunction
PrivateSubCommand2_Click()
Text1=""
Text2=""
Text3=""
EndSub
PrivateSubCommand3_Click()
End
EndSub
4.级数求和
PrivateSubCommand1_Click()
DimsAsString,nAsLong,mAsSingle,flgAsInteger
s=0
n=1
flg=1
m=1
DoUntil(Abs(m)<0.000001)
m=flg*(1/(2*n-1))
s=s+m
n=n+1
flg=-flg
Loop
Text1=CStr(4*s)
EndSub
PrivateSubCommand2_Click()
Text1=""
EndSub
PrivateSubCommand3_Click()
End
EndSub
5.矩阵靠边元素之和
PrivateSubCommand1_Click()
Dima(1To4,1To4)AsInteger
DimiAsInteger,jAsInteger
Fori=1To4
Forj=1To4
a(i,j)=Int(Rnd*10)+0
Next
Next
Fori=1To4
Forj=1To4
Picture1.Printa(i,j);
Next
Picture1.Print
Next
Text1=fun1(a)
EndSub
Functionfun1(x()AsInteger)AsDouble
DimiAsInteger,jAsInteger
fun1=0
Fori=LBound(x,1)ToUBound(x,1)
Forj=LBound(x,2)ToUBound(x,2)
Ifi=LBound(x,1)Ori=UBound(x,1)Orj=LBound(x,2)Orj=UBound(x,2)Then
fun1=fun1+x(i,j)
EndIf
Next
Next
EndFunction
PrivateSubCommand2_Click()
Picture1.Cls
Text1=""
EndSub
PrivateSubCommand3_Click()
End
EndSub
6.矩阵转置
OptionBase1
Dima(4,4)AsInteger
PrivateSubCommand1_Click()
DimbAsInteger,cAsInteger
DimiAsInteger,jAsInteger,tempAsInteger
Randomize
Forb=1To4
Forc=1To4
a(b,c)=Int(Rnd*90)+10
Text1=Text1&a(b,c)&""
Nextc
Text1=Text1&vbCrLf
Nextb
Fori=1To4
Forj=1Toi-1
temp=a(i,j)
a(i,j)=a(j,i)
a(j,i)=temp
Nextj
Nexti
Fori=1To4
Forj=1To4
Text2=Text2&a(i,j)&""
Nextj
Text2=Text2&vbCrLf
Nexti
EndSub
PrivateSubCommand2_Click()
Text1=""
Text2=""
EndSub
PrivateSubCommand3_Click()
End
EndSub
7.累加求和
PrivateSubCommand1_Click()
DimkAsString,iAsInteger,sAsInteger
k=CStr(Text1)
Fori=1ToLen(k)
s=s+Val(Mid(k,i,1))
Nexti
Text2=s
EndSub
PrivateSubCommand2_Click()
Text1=""
Text2=""
EndSub
PrivateSubCommand3_Click()
End
EndSub
8.孪生素数
PrivateSubCommand1_Click()
DimiAsInteger
List1.Clear
Fori=2To998
IfIsPrime(i)AndIsPrime(i+2)Then
List1.AddItemCStr(i)&""&CStr(i+2)
EndIf
Next
EndSub
FunctionIsPrime(ByValnAsInteger)AsBoolean
DimiAsInteger
Fori=2Ton-1
IfnModi=0ThenExitFunction
Next
IsPrime=True
EndFunction
PrivateSubCommand2_Click()
List1.Clear
EndSub
PrivateSubCommand3_Click()
End
EndSub
9.冒泡排序
OptionBase1
Dima(10)AsInteger
PrivateSubCommand1_Click()
DimiAsInteger,jAsInteger,kAsInteger,tempAsInteger
Randomize
Fori=1To10
a(i)=Int(Rnd*90)+10
Text1=Text1&a(i)&""
Nexti
Forj=1To9
Fork=1To10-j
Ifa(k)>a(k+1)Then
temp=a(k)
a(k)=a(k+1)
a(k+1)=temp
EndIf
Nextk
Nextj
Forj=1To10
Text2=Text2&a(j)&""
Nextj
EndSub
PrivateSubCommand2_Click()
Text1=""
Text2=""
EndSub
PrivateSubCommand3_Click()
End
EndSub
10.逆序数
PrivateSubCommand1_Click()
DimnAsString,sAsString
DimiAsInteger
n=Text1
Fori=Len(n)To1Step-1
s=s&Mid(n,i,1)
Nexti
Text2=s
EndSub
PrivateSubCommand2_Click()
Text1=""
Text2=""
EndSub
PrivateSubCommand3_Click()
End
EndSub
11.求不靠边元素之和
Dima(1To5,1To5)AsInteger
PrivateSubCommand1_Click()
DimiAsInteger,jAsInteger
Fori=1To5
Forj=1To5
a(i,j)=Int(Rnd*90)+10
Next
Next
Fori=1To5
Forj=1To5
Picture1.Printa(i,j);
Next
Picture1.Print
Next
EndSub
PrivateSubCommand2_Click()
Text1=fun1(a)
EndSub
Functionfun1(x()AsInteger)AsDouble
DimiAsInteger,jAsInteger
fun1=0
Fori=LBound(x,1)ToUBound(x,1)
Forj=LBound(x,2)ToUBound(x,2)
IfNot(i=LBound(x,1)Ori=UBound(x,1)Orj=LBound(x,2)Orj=UBound(x,2))Then
fun1=fun1+x(i,j)
EndIf
Next
Next
EndFunction
PrivateSubCommand3_Click()
Text1=""
Picture1.Cls
Command1.SetFocus
EndSub
PrivateSubCommand4_Click()
End
EndSub
12.求所有质因子
PrivateSubCommand1_Click()
DimaAsInteger
DimiAsInteger,sAsInteger
a=Val(Text1)
Picture1.Printa&"有质因子";
s=0
Fori=2Toa-1
DoWhileaModi=0
Ifs=0Then
Picture1.Printi;
Else
Picture1.Print","&i;
EndIf
a=a/i
s=s+1
Loop
Nexti
EndSub
PrivateSubCommand2_Click()
Text1=""
Picture1.Cls
Text1.SetFocus
EndSub
PrivateSubCommand3_Click()
End
EndSub
13.求最小公倍数
PrivateSubCommand1_Click()
DimmAsInteger,nAsInteger
m=Text1
n=Text2
Text3=m*n/gcd(m,n)
EndSub
Functiongcd(ByValaAsInteger,ByValbAsInteger)AsInteger
DimrAsInteger
Do
r=aModb
a=b
b=r
LoopUntilr=0
gcd=CStr(a)
EndFunction
PrivateSubCommand2_Click()
Text1=""
Text2=""
Text3=""
Text1.SetFocus
EndSub
PrivateSubCommand3_Click()
End
EndSub
14.水仙花数
PrivateSubCommand1_Click()
DimiAsInteger,aAsInteger,bAsInteger,cAsInteger
Fori=100To999
a=i\100
b=i\10Mod10
c=iMod10
Ifi=a*a*a+b*b*b+c*c*cThen
Text1=Text1&i&vbCrLf&“”
EndIf
Nexti
EndSub
PrivateSubCommand2_Click()
End
EndSub
15.顺序查找
Dims(1To10)AsInteger
PrivateSubCommand1_Click()
DimiAsInteger
Text1=""
Fori=1To10
s(i)=Int(Rnd*90)+10
Text1=Text1&CStr(s(i))&""
Next
EndSub
PrivateSubCommand2_Click()
DimmAsInteger
m=search(s,Val(Text2))
Ifm>0Then
Text3="要查找的数是第"&CStr(m)&"个元素"
Else
Text3="要查找的数不存在"
EndIf
EndSub
Functionsearch(x()AsInteger,ByValnAsInteger)AsInteger
DimiAsInteger
search=-1
Fori=LBound(x)ToUBound(x)
Ifx(i)=nThen
search=i
ExitFunction
EndIf
Next
EndFunction
PrivateSubCommand3_Click()
End
EndSub
16.完全因子数
PrivateSubCommand1_Click()
DimiAsInteger,jAsInteger,sAsInteger
List1.Clear
Fori=1To100
s=0
Forj=1Toi-1
IfiModj=0Then
s=s+j
EndIf
Next
Ifs=iThenList1.AddItemi
Next
EndSub
PrivateSubCommand2_Click()
List1.Clear
EndSub
PrivateSubCommand3_Click()
End
EndSub
17.字符统计
OptionBase1
PrivateSubCommand1_Click()
DimiAsInteger,a(26)AsInteger,nAsInteger
DimsAsString*1,str1AsString
str1=Text1
n=Len(str1)
Fori=1Ton
s=Mid(str1,i,1)
IfUCase(s)>="A"AndUCase(s)<="Z"Then
a(Asc(UCase(s))-64)=a(Asc(UCase(s))-64)+1
EndIf
Nexti
Fori=1To26
Ifa(i)<>0Then
List1.AddItemChr(64+i)&":
"&a(i)
EndIf
Nexti
EndSub
PrivateSubCommand2_Click()
Text1=""
List1.Clear
EndSub
PrivateSubCommand3_Click()
End
EndSub
18.最大公约数&最小公倍数
PrivateSubCommand1_Click()
DimmAsInteger,nAsInteger
m=Val(Text1)
n=Val(Text2)
Text3=gcd(m,n)
Text4=m*n/gcd(m,n)
EndSub
Functiongcd(ByValaAsInteger,ByValbAsInteger)AsInteger
DimrAsInteger
Do
r=aModb
a=b
b=r
LoopUntilr=0
gcd=CStr(a)
EndFunction
PrivateSubCommand2_Click()
Text1=""
Text2=""
Text3=""
Text4=""
Text1.SetFocus
EndSub
PrivateSubCommand3_Click()
End
EndSub
19.最大最小平均值
Dims(1To10)AsInteger
PrivateSubCommand1_Click()
DimiAsInteger
Fori=1To10
s(i)=Int(Rnd*90)+10
List1.AddItems(i)
Next
EndSub
PrivateSubCommand2_Click()
DimiAsInteger
DimmaxAsInteger,minAsInteger,sumAsInteger
max=s
(1)
min=s
(1)
sum=0
Fori=1To10
sum=sum+s(i)
Next
Fori=2To10
Ifs(i)>maxThenmax=s(i)
Ifs(i)Next
Text1=max
Text2=min
Text3=sum/10
Text4=sum
EndSub
PrivateSubCommand3_Click()
List1.Clear
Text1=""
Text2=""
Text3=""
Text4=""
Command1.SetFocus
EndSub
Fibonacci数列
PrivateSubCommand1_Click()
DimnAsInteger
n=Text1
Text2=fib(n)
EndSub
Functionfib(ByValxAsInteger)AsInteger
Ifx=1Orx=2Then
fib=1
Else
fib=fib(x-2)+fib(x-1)
EndIf
EndFunction
PrivateSubCommand2_Click()