VB编程题代码43例.doc
Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateVB编程题代码43例VB编程题代码43例VB编程题代码43例1、求100以内的素数。 Private Sub Form_Click()Dim i%, j%For i = 2 To 100For j = 2 To i - 1If i Mod j = 0 Then Exit ForNext jIf j = i Then Print iNext iEnd Sub2、从键盘输入任意长度的字符串,要求将字符顺序倒置,例如,将输入的“ABCDEFG”变换成“GFEDCBA”。 Private Sub Command1_Click()Dim a$, I%, c$, d$, n%a = InputBox$("输入字符串") n = Len(a)For I = 1 To Int(n 2)c = Mid(a, I, 1)Mid(a, I, 1) = Mid(a, n - I + 1, 1)Mid(a, n - I + 1, 1) = cNext IPrint aEnd Sub3、计算0200之间所有能被11或5整除的数之和 Private Sub Form_Click()Dim n%, i%n = 0For i = 1 To 200If i Mod 11 = 0 Or i Mod 5 = 0 Thenn = n + iEnd IfNext iPrint nEnd Sub4、输入一年份,判断它是否为闰年,并显示有关信息。(判断闰年的条件是:年份能被4整除但不能被100整除,或者能被400整除) Private Sub Command1_Click()Dim y%y = InputBox("请输入年数") If y Mod 4 = 0 And y Mod 100 <> 0 Or y Mod 400 = 0 ThenMsgBox (y & "年是闰年") ElseMsgBox (y & "年是平年") End IfEnd Sub5、已知x,y,z 3个变量中存放了3个不同的数,比较它们的大小并进行调整,使得x<y<z。 Private Sub Command1_Click()Dim x!, y!, z!x = InputBox("inputx")y = InputBox("inputy")z = InputBox("inputz")Print " x y z"If x > y Then t = x: x = y: y = tIf x > z Then t = x: x = z: z = tIf y > z Then t = y: y = z: z = tPrint "排序后" x; " " y; " " z End Sub6、求s=a+aa+aaa+.aaaaa(n个a),其中a和n的值随机产生,a的范围是1,9的整数,n的范围是5,10。如a=3,n=6,则s=3+33+333+3333+33333+333333。编程确定n和a的值,并计算s。 Private Sub Form_Click()Dim s!, t!, i!, a%, n%a = Int(Rnd * 9 + 1)n = Int(Rnd * 6 + 5)t = 0: s = 0Print "a=" a, "n=" nFor i = 1 To nt = t * 10 + as = s + tPrint t;Next iPrintPrint "s=" sEnd Sub7、计算100300之间所有能被3和7整除的数之和。 Private Sub Form_Click()For i = 100 To 300If i Mod 21 = 0 Thens = s + iPrint i;End IfNext iPrintPrint "s=" sEnd Sub8、编程求200-400范围内5的倍数或7的倍数之和。(一个数如果同时是7和5的倍数,则只能加一次。) Private Sub Command1_Click()Dim s1%,s2%,s3%,i%S1=0S2=0For i=200 to 400If i mod 5 =0 or i mod 7=0 then s1=s1+iEnd ifNext i For i=200 to 400If i mod 5 =0 or i mod 7=0 then s2=s2+iEnd ifNext iS3=s1-s2Print s3End sub9、找出被3、5、7除,余数为1的最小的5个正整数。 Private Sub Command1_Click()Dim CountN%, n%CountN = 0n = 1Don = n + 1If n Mod 3 = 1 And n Mod 5 = 1 And n Mod 7 = 1 ThenPrint nCountN = CountN + 1End IfLoop Until CountN = 5End Sub10、某次歌曲大奖赛,有7个评委。如果分别输入7个评委对某个参赛者的打分数,按照去掉一个最高分和一个最低分的计算办法,求出该参赛者的平均得分。 Private Sub Command1_Click()Dim mark!, max!, min!, aver!, i%aver = 0For i = 1 To 7mark = InputBox("输入第" & i & "位评委的打分") If i = 1 Thenmax = mark: min = markElseIf mark < min Then min = markIf mark > max Then max = markEnd Ifaver = aver + markNext iaver = (aver - min - max) / 5Print averEnd Sub11、编程显示100500之间所有的水仙花数之和。(水仙花数是3位数,其各位数之和等于该数本身) Private Sub Form_Click()Dim i%, s%, s1%, s2%, s3%, a!a = 0For i = 0 To 400s = 99 + is1 = (s Mod 100) 10s2 = s Mod 10s3 = s 100If s1 3 + s2 3 + s3 3 = s Thena = s + aEnd IfNext iPrint aEnd Sub12、随机产生一个三位正整数,然后逆序输出,产生的数与逆序数同时显示。例如,产生246,输出是642。 Dim a%, m%, b%, c%, d%Private Sub Form_Click()a = Int(Rnd * 900 + 100)Print ad = (a Mod 10) * 100b = (a Mod 100) - (a Mod 10)c = a 100m = c + b + dPrint mEnd Sub13、从键盘输入三角形的三条边a,b,c的值,根据其数值,判断能否构成三角形。 Private Sub Form_Click()Dim a%, b%, c%a = Val(InputBox("input a")b = Val(InputBox("input b")c = Val(InputBox("input c")If a + b > c And a + c > b And b + c > a ThenMsgBox ("能构成三角形") ElseMsgBox ("不能构成三角形") End IfEnd Sub14、已知数组a(),编程删除a中第5个元素。数组a中的元素分别为12,6,4,89,75,63,100,20,31。 Private Sub Form_Click()Dim a(), i%, n%a = Array(12, 6, 4, 89, 75, 63, 100, 20, 31)n = UBound(a)For i = 0 To nPrint a(i);Next iPrintFor i = 5 To na(i - 1) = a(i)Next in = n - 1ReDim Preserve a(n)For i = 0 To nPrint a(i);Next iPrintEnd Sub15、随机生成一个整型的二维数组,范围在10,20之间,以上三角形式输出该数组。(下三角、全部元素)Dim a%(4, 4) Dim a%(4, 4)Private Sub Command1_Click()Picture1.ClsFor i = 0 To 4For j = i To 4Picture1.Print Tab(j * 6); a(i, j);Next jPicture1.PrintNext iEnd SubPrivate Sub Form_Load()For i = 0 To 4For j = 0 To 4a(i, j) = Int(Rnd * 11 + 10)Next jNext iEnd Sub16、利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是20,50内的整数,输出每行中的最大值和下标。 Private Sub Form_Click()Dim a%(3, 3), s0%, s1%, s2%, s3%, b0%, b1%, b2%, b3%Max = 40For i = 0 To 3For j = 0 To 3a(i, j) = Int(Rnd * 31 + 20)Print Tab(j * 5); a(i, j);If a(0, j) >= Max Then s0 = a(0, j): b0 = jIf a(1, j) >= Max Then s1 = a(1, j): b1 = jIf a(2, j) >= Max Then s2 = a(2, j): b2 = jIf a(3, j) >= Max Then s3 = a(3, j): b3 = jNext jNext iPrintPrint " 第一行" s0; "(" 0; b0; ")" Print " 第二行" s1; "(" 1; b1; ")" Print " 第三行" s2; "(" 2; b2; ")" Print " 第四行" s3; "(" 3; b3; ")" End Sub17、利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是40,80内的整数,求它的最大值及所对应的下标。 Private Sub Form_Click()Dim a%(3, 3)Min = 80For i = 0 To 3For j = 0 To 3a(i, j) = Int(Rnd * 41 + 40)Print Tab(j * 5); a(i, j);If a(i, j) <= Min ThenMin = a(i, j): b = i: c = jEnd IfNext jNext iPrintPrint "最小值为" Min Print "其下标为" "(" b, c; ")" End Sub18、输入一系列字符串,编程求出长度最大的字符串并输出其所对应的次序。Private Sub Form_Click()Dim a(5) as string,b(4) as integer,t%,max%,imax%For i =1 to 5a(i)=inputbox("a")Next iFor i =0 to 4b(i)=len(trim(a(i)Next iMax=1For i =0 to 4If max<b(i) then max=b(i):imax=iNext iPrint "max="a(i)Print "cixu=",imax+1End sub19、利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是20,50内的整数,输出该矩阵所有数据之和 Private Sub Form_Click()Dim a%(3, 3)For i = 0 To 3For j = 0 To 3a(i, j) = Int(Rnd * 31 + 20)Print Tab(j * 5); a(i, j);Next jNext is = a(0, 0) + a(1, 1) + a(2, 2) + a(3, 3) + a(3, 0) + a(2, 1) + a(1, 2) + a(0, 3)Print s;End Sub20、输入一系列字符串,将字符串按递减次序排列。请编程实现。 Dim a() As String, n%, i%, j%Private Sub Command1_Click()n = Text1ReDim a(1 To n) As StringFor i = LBound(a) To UBound(a)a(i) = InputBox("请输入字符串") Picture1.Print a(i); Spc(1);Next iEnd SubPrivate Sub Command2_Click()For i = 1 To nFor j = 1 To n - iIf a(j) < a(j + 1) Then t = a(j): a(j) = a(j + 1): a(j + 1) = tNext jNext iFor i = 1 To nPicture2.Print a(i); Spc(1);Next iEnd SubPrivate Sub Form_Load()Text1 = ""End Sub21、随机产生10个30,100内的整数,求最大值、最小值和平均值。 Dim a(1 To 10) As Integer, amin, amax, i%, avera!Private Sub Form_Click()For i = 1 To 10a(i) = Int(Rnd * 71 + 30)Next iamin = a(1)amax = a(1)avera = a(1)For i = 2 To 10If a(i) > amax Then amax = a(i)If a(i) < amin Then amin = a(i)avera = avera + a(i)Next iFor i = 1 To 10Print a(i);Next iPrintPrint "max=" amax, "min=" amin, "avera=" avera / 10End Sub22、定义三个4×4的二维数组A,B,C,A和B中的元素均随机生成,数组A的范围是120,数组B的范围是100200,数组C是A和B相乘得到的。请编程生成并输出A,B,C。(相加)Dim a%(3, 3), b%(3, 3), c%(3, 3) Private Sub Command1_Click()Picture1.ClsFor i = 0 To 3For j = 0 To 3Picture1.Print Tab(j * 6); a(i, j);Next jPicture1.PrintNext iEnd SubPrivate Sub Command2_Click()Picture2.ClsFor i = 0 To 3For j = 0 To 3Picture2.Print b(i, j); ""Next jPicture2.PrintNext iEnd SubPrivate Sub Command3_Click()Picture3.ClsFor i = 0 To 3For j = 0 To 3Picture3.Print Tab(j * 7); c(i, j);Next jPicture3.PrintNext iEnd SubPrivate Sub Form_Load()23、随机产生15个小写字母,放在字符数组中。提示:c=chr(int(rnd*26+97) Private Sub Command1_Click()Dim c$(14), i%Picture1.ClsFor i = 0 To 14c(i) = Chr(Int(Rnd * 26 + 97)Next iFor i = 0 To 14Picture1.Print c(i);Next iEnd Sub24、随机产生10个30,100内的整数,求最大值及所对应的下标 Private Sub Form_Click()Dim a(1 To 10) As Integer, i%, maxa%, m%For i = 1 To 10a(i) = Int(Rnd * 70) + 30Next imaxa = a(1)For i = 2 To 10If a(i) > a(1) Thenmaxa = a(i): m = iEnd IfNext iFor i = 1 To 10Print a(i);Next iPrint "maxa=" & maxa; "下标为" & m End Sub25、定义三个4×4的二维数组A,B,C,A和B中的元素均随机生成,数组A范围是120,数组B的范围是100200,数组C是A和B相加得到的。请编程生成并输出A,B,C。 Private Sub Command1_Click()Dim a(3, 3), b(3, 3), c(3, 3), t%, m%For i = 0 To 3For j = 0 To 3a(i, j) = Int(Rnd * 20 + 1)Picture1.Print a(i, j);Next jNext iPicture1.PrintFor i = 0 To 3For j = 0 To 3b(i, j) = Int(Rnd * 101 + 100)Picture1.Print b(i, j);Next jNext iPicture1.PrintFor i = 0 To 3For j = 0 To 3c(i, j) = a(i, j) + b(i, j)Next jNext iEnd Sub26、利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是20,50内的整数,求它的两条对角线上元素之和。 Private Sub Form_Click()Dim a%(3, 3)For i = 0 To 3For j = 0 To 3a(i, j) = Int(Rnd * 31 + 20)Print Tab(j * 5); a(i, j);Next jNext is = a(1, 1) + a(2, 2) + a(3, 3) + a(0, 0) + a(0, 3) + a(1, 2) + a(2, 1) + a(3, 0)PrintPrint "两对角线上元素之和为:" s End Sub27、利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是40,80内的整数,求它的最小值及所对应的下标。 Private Sub Form_Click()Dim a%(3, 3)Min = 80For i = 0 To 3For j = 0 To 3a(i, j) = Int(Rnd * 41 + 40)Print Tab(j * 5); a(i, j);If a(i, j) <= Min ThenMin = a(i, j): b = i: c = jEnd IfNext jNext iPrintPrint "最小值为" Min Print "其下标为" "(" b, c; ")" End Sub28、随机生成一个整型的二维数组,范围在10,20之间,以上三角形式输出该数组。(下三角、全部元素)Dim a%(4, 4) Dim a%(4, 4)Private Sub Command1_Click()Picture1.ClsFor i = 0 To 4For j = i To 4Picture1.Print Tab(j * 6); a(i, j);Next jPicture1.PrintNext iEnd SubPrivate Sub Form_Load()For i = 0 To 4For j = 0 To 4a(i, j) = Int(Rnd * 11 + 10)Next jNext iEnd Sub29、有10个评委对歌手进行打分(分数存在A数组中)要求编程计算出歌手平均得分(按去掉一个最高分和一个最低分的计算方法计算。)a = Array(98, 97, 95, 91, 90, 99, 93, 94, 93, 96) Private Sub Form_Click()Dim a(), max%, min%, imax%, imin%, s!a = Array(98, 97, 95, 91, 90, 99, 93, 94, 93, 96)n = UBound(a)For i = 0 To nPrint a(i);Next iPrintmin = a(0): imin = 0For i = 0 To nIf a(i) < min Then min = a(i): imin = iNext iPrint "min=" "imin=" iminFor i = imin + 1 To na(i - 1) = a(i)Next iReDim Preserve a(n - 1)For i = 0 To n - 1If a(i) > max Then max = a(i): imax = iNext iPrint "max=" "imax=" imaxReDim Preserve a(n - 2)s = 0For i = 0 To n - 2s = s + a(i)Print a(i);Next iPrintPrint "avr=" s / (n - 1)End Sub30、编程输出n行杨辉三角图形,n由键盘输入。 Private Sub Command1_Click()n = Text1.TextReDim a(1 To n, 1 To n)For i = 1 To nFor j = 1 To na(i, 1) = 1If i = j Then a(i, j) = 1Next jNext iFor i = 2 To nFor j = 2 To na(i, j) = a(i - 1, j - 1) + a(i - 1, j)Next jNext iFor i = 1 To nFor j = 1 To iPicture1.Print a(i, j); " "Next jPicture1.PrintNext iEnd Sub31、声明一个整型的二维数组a(1 to 4,1 to 4),用随机函数产生各元素,范围介于1,20之间,编程将第1行和第3行对应元素交换。 Dim a%(1 To 4, 1 To 4), t%Private Sub Command1_Click()Picture1.ClsFor i = 1 To 4For j = 1 To 4Picture1.Print Tab(j * 4); a(i, j);Next jPicture1.PrintNext iEnd SubPrivate Sub Command2_Click()For i = 1 To 4For j = 1 To 4t = a(1, j): a(1, j) = a(3, j): a(3, j) = tPicture2.Print Tab(j * 4); a(i, j);Next jPicture2.PrintNext iEnd SubPrivate Sub Form_Load()For i = 1 To 4For j = 1 To 4a(i, j) = Int(Rnd * 20 + 1)Next jNext iEnd Sub32、随机产生15个小写字母,放在字符数组中。提示:c=chr(int(rnd*26+97) Private Sub Command1_Click()Dim c$(14), i%Picture1.ClsFor i = 0 To 14c(i) = Chr(Int(Rnd * 26 + 97)Next iFor i = 0 To 14Picture1.Print c(i);Next iEnd Sub33、编写一个过程计算并输出区间100,1000内所有素数的和(要求判断素数使用过程(函数或子过程)来实现。调用该过程。(提示:只能被1和自身整除的自然数称为素数。) Function sushu%(ByVal x%)Dim m%, Tag As Boolean, i%m = Val(x)Tag = TrueFor i = 2 To m - 1If (m Mod i) = 0 Then Tag = FalseNext iIf Tag Then sushu = xEnd FunctionPrivate Sub Command1_Click()Dim i%, k%For i = 100 To 1000If sushu(i) Then List1.AddItem iNext iSum = 0For k = 0 To List1.ListCount - 1Sum = Sum + Val(List1.List(k)Next kLabel1 = "100,1000内所有素数和为:" & Sum End Sub34、随机产生10个1100的正整数放入数组,显示产生的数,求最大值、最小值、平均值。 Private Sub Form_Click()Dim a(1 To 10) As Integer, i%, maxa%, mina%, suma%For i = 1 To 10a(i) = Int(Rnd * 71 + 30)Next imina = a(1)maxa = a(1)suma = a(1)For i = 2 To 10If a(i) > maxa Then maxa = a(i)If a(i) < mina Then mina = a(i)suma = suma + a(i)Next iFor i = 1 To 10Print a(i);Next iPrintPrint "max=" maxa, "min=" mina; "aver=" suma / 10End Sub35、已知一维数组a()中的元素已排序,分别为12,15,21,25,27,35,36,39,48,52,编程将30插入数组a,并使a依然有序。 Private Sub Command1_Click()Dim a(), i%, k%, x%, n%a = Array(12, 15, 21, 25, 27, 35, 36, 39, 48, 52)n = UBound(a)x = Val(Text1)For k = 0 To nIf x < a(k) Then Exit ForNext kReDim Preserve a(n + 1)For i = n To k Step -1a(i + 1) = a(i)Next ia(k) = xFor i = 0 To n + 1Print a(i);Next iEnd Sub37、声明一个整型的二维数组a(1 to 4,1 to 4),用随机函数产生各元素,范围介于1,20之间,编程将第2行和第4行对应元素交换。Private Sub Form_Click()Dim a(0 To 3, 0 To 3), i%, j%, tFor i = 0 To 3For j = 0 To 3a(i, j) = Int(Rnd * 21 + 1)Print Tab(j * 5); a(i, j)Next jNext iPrintPrintFor j = 0 To 3t = a(1, j): a(1, j) = a(3, j): a(3, j) = tNext jFor i = 0 To 3For j = 0 To 3Print Tab(j * 5); a(i, j)Next jNext iEnd Sub38、随机产生10个30,100内的整数,求最小值及所对应的下标。 Private Sub Form_Click()Dim a(1 To 10) As Integer, i%, mina%, m%For i = 1 To 10a(i) = Int(Rnd * 70) + 30Next imina = a(1)For i = 2 To 10If a(i) < a(1) Thenmina = a(i): m = iElseIf a(i) = a(1) Thenmina = a(i): m = iEnd IfNext iFor i = 1 To 10Print a(i);Next iPrint "mina=" & mina; "下标为" & m End Sub39、编一个判断宿舍的通用过程(函数或子过程)。调用该过程,计算并输出区间(1000,,1100)内所有素数的和。 Function ss(n) As IntegerDim i%, tag As Booleantag = TrueFor i = 3 To n / 2If (n Mod i) = 0 Then tag = FalseNext iIf tag Then ss = nEnd FunctionPrivate Sub Picture1_Click()Dim i%, averaavera = 0For i = 1000 To 1100If ss(i) <> 0 Then Print ss(i)avera = avera + ss(i)Next iPicture1.Print averaEnd Sub40、编一子过程ProcMin,求一维数组a中的最小值,子过程的形参自己确定。 Private Sub Form_Click()Dim a(1 To 10), amin, i%For i = 1 To 10a(i) = Int(Rnd * 101)Print a(i);Next iCall ProcMin(a(), amin)PrintPrint "amin =" aminEnd SubSub ProcMin(b(), min)Dim i%min = b(LBound(b)For i = LBound(b) + 1 To UBound(b)If b(i) < min Then min = b(i)Next iEnd Sub41、编一个冒泡排序法子过程,对已知的若干整数按递减次序排列。提示:子过程的形式为sub sort1(a%()。提示:子过程的形式为sub sort1(a%()。 Private Sub Command1_Click()Dim b%(10), i%Print "排序前" For i = 0 To 9b(i) = Int(Rnd * 100)Print b(i);Next iPrintPrint "排序后" Call sort(b()End SubSub sort(a%()Dim i%, m%, n%, min%, j%n = UBound(a)For i = 0 To n - 1For j = 0 To n - 1 - iIf a(j) < a(j + 1) Then m = a(j): a(j) = a(j + 1): a(j + 1) = mNext jNext iFor i = LBound(a) To UBound(a) - 1Print a(i);Next iEnd Sub42、编一个选择排序法子过程,对已知的若干整数按递增次序排列。提示:子过程的形式为sub sort1(a%()。 Private Sub Command1_Click()Dim b%(10), i%Print "排序前" For i = 1 To 10b(i) = Int(Rnd * 100)Print b(i);Next iPrintPrint "排序后" Call sort(b()End SubSub sort(a%()Dim i%, m%, n%