您好,欢迎访问三七文档
一、求和、求均值,最大、最小值;1.求和及平均值(1)每一行的和及平均值Dimsumasinteger,sum1asintegerFori=1To4sum=0Forj=1To4sum=sum+a(i,j)NextjPrintsum/4---每行平均值Nexti2.找最值DimmaxAsInteger,minAsIntegermax=a(1):min=a(1)Fori=2To10Ifmax=a(i)Thenmax=a(i)EndIfIfmin=a(i)Thenmin=a(i)EndIfNextiPrintmax;min二、判断字母、数字、其他字符(1)If...ElseIf...:PrivateSubCommand1_Click()DimaAsString*1a=Text1IfUCase(a)=AAndUCase(a)=ZThenText1.Text=a&是字母ElseIfa=0Anda=9ThenText1.Text=a&是数字ElseText1.Text=a&是其它字符EndIfEndSub(2)SelectCase:PrivateSubCommand2_Click()DimaAsString*1a=Text1SelectCaseaCaseaToz,AToZText1.Text=a&是字母Case0To9Text1.Text=a&是数字CaseElseText1.Text=a&是其它字符EndSelectEndSub三、计数(统计各个字母个数、各个数字个数)(1)统计各个字母个数:【实验十】2.PrivateSubForm_Click()FontSize=20ClsDima%(65To90),b$,cb=InputBox(输入一串字符串(字母):)Print输入的字母串为;bFori=1ToLen(b)c=Asc(UCase(Mid(b,i,1)))a(c)=a(c)+1NextiFori=65To90Ifa(i)0ThenPrintChr(i);的个数为:;a(i)NextiEndSub(2)各个数字个数编写一程序,用随机函数产生〔0-99〕范围的随机整数,统计个位上数字分别为1,2,3,4,5,6,7,8,9,0的数的个数。PrivateSubForm_Click()Dima(100),b(100)AsIntegerFori=1To100a(i)=Int(Rnd*100)Printa(i);IfiMod10=0ThenPrintEndIfNextFori=1To100b(i)=a(i)Mod10Printb(i);IfiMod10=0ThenPrintEndIfSelectCaseb(i)Caseb(i)=0a0=a0+1Caseb(i)=1a1=a1+1Caseb(i)=2a2=a2+1Caseb(i)=3a3=a3+1Caseb(i)=4a4=a4+1Caseb(i)=5a5=a5+1Caseb(i)=6a6=a6+1Caseb(i)=7a7=a7+1Caseb(i)=8a8=a8+1Caseb(i)=9a9=a9+1EndSelectNextiPrintPrinta0=;a0Printa1=;a1Printa2=;a2Printa3=;a3Printa4=;a4Printa5=;a5Printa6=;a6Printa7=;a7Printa8=;a8Printa9=;a9EndSub四、回文数、水仙花数、百元买百鸡(1)回文数(设n是一任意自然数。若将n的各位数字反向排列所得自然数n1与n相等,则称n为一回文数。例如,若n=1234321,则称n为一回文数;但若n=1234567,则n不是回文数。)PrivateSubForm_Load()DimlAsIntegera=Int(InputBox(请输入一个正整数))l=Len(a)Fori=1Tol/2IfMid(a,i,1)=Mid(a,l+1-i,1)ThenClsPrintPrintTab(12);a&是回文数ElseClsPrintPrintTab(12);a&不是回文数EndIfNextiEndSub(2)水仙花数:水仙花数是指一个n位数(n≥3),它的每个位上的数字的n次幂之和等于它本身。(例如:1^3+5^3+3^3=153)PrivateSubCommand1_Click()DimxAsIntegerDimg%,s%,b%x=153g=xMod10s=x\10Mod10b=x\100Ifg^3+s^3+b^3=xThenForm1.Print水仙花数ElseForm1.PrintnoEndIfEndSub(3)百元买百鸡问题:编写程序解决百钱买百鸡问题:公鸡5元/只,母鸡3元/只,小鸡一元买3只,用100元买100只鸡,问有哪些购买方案?(p34)PrivateSubCommand1_Click()Dima,b,cAsIntegerDimaconst,bconstAsIntegerDimsumAsIntegeraconst=Int((100-(100/3))/5)+1Fora=0Toaconstbconst=Int((100-a*5)/3)+1Forb=0Tobconstc=100-a-bIfNot(cMod3)Thensum=a*5+3*b+c/3Ifsum=100ThenPrint公鸡&a,母鸡&b,小鸡&cElseIfsum100ThenExitForEndIfEndIfNextNextEndSub五、数组排序(冒泡、选择),交换(1)数组排序(冒泡、选择)1.冒泡:PrivateSubCommand2_Click()FontSize=15ClsPrint随机产生的原数组为:n=InputBox(数组元素的个数N:)ReDima(n)Fori=1ToUBound(a)a(i)=Int(Rnd*200+1)Printa(i);NextiPrintFori=1ToUBound(a)-1p=iForj=i+1ToUBound(a)Ifa(j)a(p)Thenp=jNextjt=a(i)a(i)=a(p)a(p)=tNextiPrint排序后数组为:Fori=1ToUBound(a)Printa(i);NextiPrintEndSub2.选择:PrivateSubCommand1_Click()FontSize=15ClsPrint随机产生的原数组为:n=InputBox(数组元素的个数N:)ReDima(n)Fori=1ToUBound(a)a(i)=Int(Rnd*200+1)Printa(i);NextiPrintFori=1ToUBound(a)-1p=iForj=i+1ToUBound(a)Ifa(j)a(p)Thenp=jNextjt=a(i)a(i)=a(p)a(p)=tNextiPrint选择法排序后数组为:Fori=1ToUBound(a)Printa(i);NextiPrintEndSub(2)交换1.【实验七】1.PrivateSubForm_Click()Dimi%,a$,b$,c$a=InputBox(请输入一个任意整数:)Print输入的整数为:;aFori=1ToLen(a)b=Mid(a,i,1)Printb&,;c=b&cNextiPrint逆向排列数为:;cEndSub2.【实验十】1.1PrivateSubForm_Click()FontSize=20ClsDimi%,a%(10),t%RandomizePrint随机产生的10个数字为:Fori=1To10a(i)=Int(Rnd*101+200)Printa(i);IfiMod5=0ThenPrintNextiFori=1To5t=a(i):a(i)=a(i+5):a(i+5)=tNextiPrint交换后的数字为:Fori=1To10Printa(i);IfiMod5=0ThenPrintNextiEndSub六、阶乘、阶乘和组合数。(1)阶乘1.X%=inputbox(“”)6DimsasintegerS=0Fori=1tox-15Ifxmodi=0thenS=s+i0+1+2+3EndifNexti2345Ifs=xthenPrint“yes”ElsePrint“no”Endif2.【实验十三】PrivateSubform_Click()Dima%,s!FontSize=20n=InputBox(输入一个正整数:)s=fac(n)Printn&!=&sEndSubPublicFunctionfac(k)AsSingleDimi%fac=1Fori=1Tokfac=fac*iNextiEndFunction(2)阶乘PrivateSubForm_Click()DimxAsLong,yAsDouble,zAsDoublePrintTab(15);1-10的阶乘y=1Forx=1To10y=y*x:z=z+yPrintx;!=;y:NextxPrint1!+2!+3!+4!+5!+6!+7!+8!+9!+10!=;zEndSub3.阶乘和PrivateSubCommand1_Click()Fori=1To10Sum=Sum+jiecheng(i)NextPrintSumEndSubFunctionjiecheng(num)jiecheng=1Fori=1Tonumjiecheng=jiecheng*iNextEndFunction七、素数;斐波那契数列(1)斐波那契数列:1.题目:输出斐波那契级数1、2、3、5、8、13….,当某项数值大于或等于30000时结束。此级数项的规律是:前两项的值各为1,从第3项起,每一项是前两项的和。(要求一行输出6项)。PrivateSubForm_Click()Dima&,b&,c&,i%a=1b=1i=2Printa;b;Doc=a+bIfc=30000ThenPrintc;ElseExitDoEndIfa=bb=ci=i+1IfiMod6=0ThenPrintLoopEndSub2.题目:输出斐波那契级数1、1、2、3、5…..的前30项要求每行输出5个(用数组完成)。【提示】斐波那契级数列的特点是:第一、第二都是1,从第三项开始,前面相邻两项之和,构成后一项。即:a(n)=a(n-1)+a(n-2).PrivateSubForm_Click()FontSize=20Dima&()ReDima(2)a(1)=1:a(2)=1ClsPrinta(1);a(2);i=3DoReDimPreservea(i)a(i)=a(i-2)+a(i-1)Printa(i);IfiMod5=0ThenPrintIfi=30ThenExitDoi=i+1LoopEndSub(2)素数:方法一:一个数n是素数的条件:不能被2~n-1整除用For…….Next语句DimIasInteger,NAsIntegerN=val(InputBox())ForI=2toN-1IfNModI=0ThenExitFor'如果能被2~N-1中任何一个数整除,则不是素数,跳出For循环NextIIfI=NThen'如果正常跳出For循环,则I跳出循环For后的值应该是N,所以满足这个条件PrintN&是素数ElsePrintN&不是素数EndIf方法二:(一般都是用这种方法)一个数n是素数的条件:不能被2~Sqr(n)整除,1、用For…….Next语句DimIasInteger,NAsIntegerN=val(InputBox())ForI=2toint(Sqr(N))IfNModI=0ThenExitForNextIIfIint(Sqr(N))ThenPrintN&是素数ElsePrintN&不是素数EndIf2.【实验十三过程】PrivateSubForm_Click()Dimn%FontSize=20n=InputBox(请输入一个正整数n:)IfPrime(n)=TrueThenPrintn;是素数ElsePri
本文标题:VB程序编辑题
链接地址:https://www.777doc.com/doc-2865886 .html