经典的程序设计题

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

累加求和1到100PrivateSubCommand1_Click()DimiAsInteger,sumAsIntegersum=0Fori=1To100sum=sum+iNextText1.Text=sumEndSubPrivateSubCommand2_Click()EndEndSub1到100的奇数和PrivateSubCommand1_Click()DimiAsInteger,sumAsIntegersum=0Fori=1To100step2sum=sum+iNextText1.Text=sumEndSubPrivateSubCommand2_Click()End求n!PrivateSubCommand1_Click()DimfactAsDouble,nAsInteger,iAsIntegern=Val(Text1.Text)fact=1Fori=1Tonfact=fact*iNextText2.Text=factEndSub计算人口PrivateSubLabel1_Click()DimpAsSingle,rAsSingle,iAsSinglep=14r=0.012i=0Whilep=20p=p*(1+r)i=i+1WendPrint:PrintPrinti;年后,我国人口将达到;p;亿EndSub累加求和1到100(Dowhile)PrivateSubCommand1_Click()DimiAsInteger,sumAsIntegersum=0i=1DoWhilei=100sum=sum+ii=i+1LoopText1.Text=sumEndSubPrivateSubCommand2_Click()EndEndSub(DoUntil)PrivateSubCommand1_Click()DimiAsInteger,sumAsIntegersum=0i=1Dountili100sum=sum+ii=i+1LoopText1.Text=sumEndSubPrivateSubCommand2_Click()EndEndSub求平均成绩PrivateSubCommand1_Click()DimaAsSingle,bAsSingle,cAsSingle,dAsSingleDimpjAsSinglea=60:b=70:c=80:d=90pj=(a+b+c+d)/4Text1.Text=Str(pj)EndSub成绩鉴定PrivateSubCommand1_Click()DimxAsSinglex=Text1.TextIfx=90ThenText2.Text=优秀ElseIfx=80ThenText2.Text=良好ElseIfx=70ThenText2.Text=中ElseIfx=60ThenText2.Text=及格ElseText2.Text=不及格EndIfEndSub求圆的周长PrivateSubCommand1_Click()DimrAsDoubleConstpi=3.1415926r=Val(InputBox(请输入圆的半径))Print圆的周长为:,2*pi*rEndSub输入两个整数mn,使得m=n,并输出mnPrivateSubCommand1_Click()DimmAsInteger,nAsInteger,aAsIntegerm=Val(InputBox(请输入m:,输入,m))n=Val(InputBox(请输入n:,输入,n))IfnmThena=mm=nn=aEndIfPrintm=,mPrintn=,nEndSub计算sum=1/1!+1/2!......+1/10!PrivateSubCommand1_Click()DimmAsInteger,nAsLong,sAsDoublen=1s=0Form=1To10n=n*ms=s+1/nNextPrintsEndSub定义一个下标上界为10的整型数组,并随机函数为其赋值(值处于【0,100】之间,计算数组中所有偶数的和PrivateSubCommand1_Click()RandomizeDima%(10)Dimi%,s%Fori=0To10a(i)=101*Rnd()NextFori=0To10Ifa(i)Mod2=0Thens=s+a(i)EndIfNextPrint所有偶数的和为:,sEndSub输入一个四位数,分别输出它的个位数字、十位数字、百位数字、和千位数字。(待修改)PrivateSubCommand1_Click()Dima%,i%,k%,m%,s$a=Val(InputBox(请输入一个四位数))i=aMod10j=(aMod100)\10k=(aMod1000)\100m=a\1000s=个位数字是:&i&vbcrlf十位数字是:&j&vbcrlf&百位数字是:&k&vbcrlf&千位数字是:&mPrintsEndSub计算sum=2!+4!+6!+......+10!PrivateSubCommand1_Click()Dima!,i%,sum!i=1:a=1Doa=2*iIfiMod2=0Thensum=sum+aEndIfi=i+1LoopWhilei=10PrintsumEndSub用inputbox输入一个年份,判断该年份是否是闰年,若是则输出Yes,否则输出NoPrivateSubCommand1_Click()Dimj%j=Val(InputBox(请输入年份))IfjMod400=0OrjMod4=0AndjMod1000ThenMsgBox(Yes)ElseMsgBox(No)EndIfEndSub计算并输出下面级数前n项(n=20)中奇数项的和。1*2*3-2*3*4+3*4*5-4*5*6+........+(-1)……(n-1)*n*(n+1)*(n+2)+.....PrivateSubCommand1_Click()Dims!,i%Fori=1To20Step2s=s+i*(i+1)*(i+2)NextMsgBox(s)EndSub如果一个三位数等于其各位数字的立方和,则称其为水仙花数。请输入一个三位数,并判断其是否为水仙花数。若是,用MsgBox输出“yes”,否则输出“no”PrivateSubCommand1_Click()Dima%,i%,j%,k%a=InputBox(请输入一个三位数!)i=a\100j=a\10Mod10k=aMod10Ifa=i^3+j^3+k^3ThenMsgBox(yes)ElseMsgBox(no)EndIfEndSub某分段函数描述如下:(1)当x为偶数时,y的值为x的二分之一(2)当x为奇数时,y的值为x的二倍PrivateSubCommand1_Click()Dimx!,y!x=Val(InputBox(请输入X的值!))IfxMod2=0Theny=x/2Elsey=2*xEndIfMsgBox(y)EndSub输入三个数,判断其是否能够构成三角形...........PrivateSubCommand1_Click()DimiAsInteger,a(2)AsInteger,pAsSingle,sAsSingleFori=0To20a(i)=val(inputbox(输入第&i&条边的边长:))NextiIfa(0)+a(1)a(2)Anda(0)+a(2)a(1)Anda(1)+a(2)a(0)Thenp=(a(0)+a(1)+a(2))/2s=sqr(p*(p-a(0)*(p-a(1))*(p-a(2)))MsgBox(s)ElseMsgBox(不能形成三角形!)EndIfEndSub计算1*3*5....*9PrivateSubCommand1_Click()DimsumAsLong,iAsIntegersum=1Fori=1To9Step2sum=sum*iNextMsgBox(sum)EndSub随机产生一个小写字母PrivateSubCommand1_Click()DimaAsStringRandomizea=Chr(Int(97+26*Rnd))MsgBox(a)EndSub编写程序,计算下面级数中偶数项的和ES,在求和的过程中,当某偶数项(注意,该项参与求和)的值0.001时求和终止并输出ES,结果取6位1/(1*2)+1/(2*3)+1/(3*4)……+1/(n*(n+1)privateSubCommand1_Click()Dima#,i%,ES#i=1Doa=i*(i+1)i=i+1IfiMod2=0ThenES=ES+1/aEndIfLoopUntil1/a0.001ES=fonnat(ES,0.000000)PrintESEndSub编写程序,求下面级数前n项的和s=1/1!-1/2!+1/3!-1/4………(-1)∧(n-1)*1/n!+…..求和过程当某一项的绝对值小于0.0001时结束(注意,该项参与求和),计算并输出所求的和s,要求保留6位小数PrivateSubCommand1_Click()DimmAsDouble,nAsInteger,sAsDouble,fAsIntegers=0:n=-1:m=1:f=1DoWhile1/m=0.0001m=m*ff=f+1n=-ns=s+n/mLoops=Format(s,0.000000)PrintsEndSub随即输出一个位于【0,100】之间的整数PrivateSubCommand1_Click()RandomizeDims%s=(101*Rnd)PrintsEndSub编写程序,计算并输出下面级数前n项(n=50)中的偶数项的和PrivateSubCommand1_Click()Dims!,i%Fori=2To50Step2s=s+i*(i+1)NextMsgBox(s)EndSub输入长方形的长和宽,计算长方形的周长,并输出PrivateSubCommand1_Click()Dimm#,n#m=Val(InputBox(请输入长方形的边长))n=Val(InputBox(请输入长方形的另一个边长))MsgBox(2*(m+n))EndSub《VB期末范围题总汇》1.求随机10个整数的最大值、最小值、平均值以及和;Dima(1To10)PrivateSubCommand1_Click()RandomizePicture1.Print产生的随机数为:Fori=1To10a(i)=Int(Rnd*99+1)Picture1.Printa(i);NextiPicture1.PrintEndSubPrivateSubCommand2_Click()Dimmax,min,avemax=a(1)min=a(1)ave=0.1*a(1)Fori=2To10Ifa(i)maxThenmax=a(i)Ifa(i)minThenmin=a(i)ave=ave+0.1*a(i)NextiPicture1.Print最大数为:;maxPicture1.Print最小数为:;minPicture1.Print平均数为:;aveEndSub2.求水仙花数PrivateSubForm_Click()Dima,b,cAsInteger'a(个)b(十)c(百)Fora=0To9Forb=0To9Forc=1To9Ifa^3+b^3+c^3=a+10*b+100*cThenMsgBox100*c+10*b+aEndIfNextcNextbNextaEndSub3.百元买百鸡问题;OptionExplicitConsta=5,b=3,c=1PrivateSubForm_Activate()DimiAsInteger,jAsInteger,kAsInteger,nAsIntegerFori=1To100Forj=1To100Fork=1To100Ifi*a+j*b+k*c=100Thenn=n+1List1.AddItem公鸡:&i&母鸡:&j&小鸡:&kEndIfNe

1 / 15
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功