资源描述
)1、随机产生三个 100300 之间的整数,判断这三个整数是否能构成三角形,如果可以,求 三角形的面积。Dim x As Integer, y As Integer, z As IntegerDim c As Single, s As Singlex = Int(Rnd * 201 + 100)y = Int(Rnd * 201 + 100)z = Int(Rnd * 201 + 100)If x + y z And x + z y And y + z x Thenc = (x + y + z) / 2s = Sqr(c * (c - x) * (c - y) * (c - z)Print 三角形面积是: & sElsePrint 构不成三角形End If2、随机产生 0100 之间的 60 名学生的数学分数,分别统计分数在0,10)、10,20)、20,30)、30,40、40,50、50,60、60,70、70,80、80,90)、90,100、之间的学生人数。Dim a%, b%, c%, d%, e%, f%, g%, h%, i%, j% Dim x As SingleFor m = 1 To 60 产生 60 名学生的分数 x = Rnd * 101 产生 0100 的分数 Print x;If x = 90 Thena = a + 1ElseIf x = 80 Thenb = b + 1ElseIf x = 70 Thenc = c + 1ElseIf x = 60 Thend = d + 1ElseIf x = 50 Thene = e + 1ElseIf x = 40 Thenf = f + 1ElseIf x = 30 Theng = g + 1ElseIf x = 20 Thenh = h + 1ElseIf x = 10 Theni = i + 1Elsej = j + 1End IfNext mPrint a, b, c, d, e, f, g, h, i, j3、我国有 13 亿人口,按照人口年增长 0.8%计算,多少年以后我国人口超过 26 亿。Dim x As Double 人数一定要定义成双精度的Dim n As Integerx = 1300000000n = 0Do While x = 0 Thenx1 = (-b + Sqr(d) / (2 * a) 求方程的两个根x2 = (b + Sqr(d) / (2 * a)x1 = Format(x1, .#) 对根规范化输出x2 = Format(x2, .#)End IfPrint x1, x25、某次歌手大奖赛,共有 10 名选手,有 10 名评委打分。要求评委给 10 位选手打分,去掉 一个最高分,去掉一个最低分,求出该选手的平均分。Dim i As IntegerDim j As IntegerDim max As Single, min As Single, sum As Single, aver As SingleFor i = 1 To 10 十个选手的成绩max = 0min = 100sum = 0For j = 1 To 10 十个评委的分数x = Rnd * 101 产生 0100 的分数If x max Then max = xIf x min Then min = xsum = sum + x)Next javer = (sum - max - min) / 8Print aver;Next i6、求 1 到 100 以内的素数。Dim i As Integer, j As Integer, prime As BooleanFor i = 1 To 100prime = TrueFor j = 2 To Sqr(i) 判断一个数是否是素数If i Mod j = 0 Thenprime = FalseEnd IfNext jIf prime Then 如果 prime = True 则 i 是素数Print i & 是素数ElsePrint i & 不是素数 如果 prime = False 则 i 不是素数End IfNext i7、求出 100 以内的所有勾股数(勾股数为 a2+b2=c 2,c 为自然数,且 ab) Dim a As Integer, b As Integer, c As IntegerFor a = 1 To 100For b = 1 To 100For c = 1 To 100If c * c = a * a + b * b And a b ThenPrint a; b; c;PrintEnd IfNext cNext bNext a8、把输入的字符串逆序输出。Dim str As String, strRe As StringDim i As Integerstr = Text1.Text 原字符串For i = 1 To Len(str)strRe = Mid(str, i, 1) & strRe 字符串逆序Next iText2.Text = strRe 逆序后的字符串9、随机产生 0100 之间的 60 名学生的数学分数,分别统计分数在0,10)、10,20)、20,30)、30,40)、40,50)、50,60、60,70)、70,80、80,90)、90,100、之间的学生人数。(用数组实现)60,100 Dim i As Integer, s(1 To 10) As Integer, mark(1 To 60) As Integer, For i = 1 To 60mark(i) =int( Rnd * 101) 随机产生 0100 之间的分数 Print mark(i); 输出 60 个分数Select Case mark(i)Case Is 10s(1) = s(1) + 1Case Is 20s(2) = s(2) + 1Case Is 30s(3) = s(3) + 1Case Is 40s(4) = s(4) + 1Case Is 50s(5) = s(5) + 1Case Is 60s(6) = s(6) + 1Case Is 70s(7) = s(7) + 1Case Is 80s(8) = s(8) + 1Case Is 90s(9) = s(9) + 1Case Is max Then max = mark(i) 找出最大数If mark(i) mark(n + 1) Then mark(n + 2) = mark(n + 2) + 1Print mark(i); 输出学生成绩Next iPrint mark(n + 1); mark(n + 2) 输出平均分与高于平均分的人数,求平均分,并12、随机生成包含 10 个数组元素的有序数组,然后第一个与第六个进行交换,第二个与第 七个进行交换。,并把原数组以及交换后的数组分别在窗体上显示出来。Dim s(1 To 10) As IntegerFor i = 1 To 10 产生 10 个数s(i) = Int(Rnd * 101)Print s(i); 输出交换前的数值Next iPrintFor i = 1 To 5 实现交换t = s(i)s(i) = s(5 + i)s(5+ i) = tNext iFor i = 1 To 10Print s(i); 输出交换后的数值Next i13、已知数组 a=Array(1,4,8,5,10),b(6),通过数组 a 给数组 b 赋值;用选择法按照升序对数 组 b 排序,对排序后的数组插入元素 6,使 b 数组有序;删除元素 5,并使数组元素个数减 1。O ption Base 1P rivate Sub Command1_Click()Dim b(1 To 6) As Integer, a As Varianta = Array(1, 4, 8, 5, 10)For i = 1 To 5 用数组 A 给数组 B 赋值b(i) = a(i)Print b(i); 输出数组 B 的前 5 个元素Next iPrint b(6); 输出数组 B 最后一个元素PrintFor i = 1 To 5 用选择法对数组 B 升序排序Min = iFor j = i + 1 To 6If b(j) b(Min) Then Min = jNext jt = b(i): b(i) = b(Min): b(Min) = tNext iFor i = 1 To 6 输出数组 B 的所有元素Print b(i);Next iPrint下面是对有序数组 B 插入元素 6For j = 2 To 6If 6 b(j - 1) Then 找出插入位置For i = 1 To j - 1 插入位置以前的元素向前移动一个位置 b(i) = b(i + 1)Next ib(j - 1) = 6 在准确的位置插入 6End IfNext jFor i = 1 To 6 输出插入 6 以后的数组 B 的所有元素 Print b(i);Next iPrintDim c() As Integer 定义数组 C 是一个动态数组ReDim c(1 To 6) As IntegerFor i = 1 To 6c(i) = b(i)Next i下面是删除元素 5For i = 1 To 6If c(i) = 5 Then 如果数组 C 里面有 5 就进行删除For j = i To 5 把元素 5 后面的元素向前移动一个位置c(j) = c(j + 1)Next jEnd IfNext iReDim Preserve c(5) 只保留数组 C 里面前面的 5 个元素,达到了删除目的For i = 1 To 5Print c(i); 输出删除 5 以后的数组元素Next iEnd Sub14、编写一个无参数 Sub 过程 Triangle,在窗体上输出用“*”组成的如下图所示的三角形图 案。被调过程Public Sub Triangle() 子过程Print *Print * * *Print * * * * *Print * * * * * * * Print * * * * * * * * *End Sub主调过程Private Sub Command1_Click()Call Triangle 调用子过程End Sub15、把上面的 Triangle 过程改造成带参数的过程,使之能输出任意行三角形图案,并尝试用 两种方法调用。被调过程Public Sub Triangle(n As Integer)For i = 1 To nPrint Tab(n - i); String(2 * i - 1, *);Next iEnd Sub主调过程Private Sub Command1_Click()Dim a As Integera = Val(InputBox(请输入 a)Call Triangle(a)End Sub16、编写一个函数 Prime,用于判断一个自然数是否为素数。调用此函数输出 3100 之间的 所有的素数。(提示:函数 Prime 的类型使用布尔型)被调函数Public Function Prime(m As Integer, n As Integer) As BooleanDim i As Integer, j As Integer判断 m-n 之间的所有素数For i = m To nPrime = TrueFor j = 2 To Sqr(i) 判断一个数是否是素数If i Mod j = 0 ThenPrime = FalseEnd IfNext jIf Prime Then 如果 prime = True 则 i 是素数Print i & 是素数ElsePrint i & 不是素数 如果 prime = False 则 i 不是素数End IfNext iEnd Function主调过程Private Sub Command1_Click()Dim a As Integer, b As IntegerDim c As Booleana = Val(InputBox(请输入 a)b = Val(InputBox(请输入 b)c = Prime(a, b)End Sub17、编写函数,判断某数是否是“水仙花数”。并用此函数输出 100999 之间的所有的水仙 花数、所谓的“水仙花数”是指一个 3 位数,其各位数字立方和等于该数本身。主调函数Private Sub Command1_Click()Dim a As Integer, b As IntegerDim c As Booleana = Val(InputBox(请输入 a)b = Val(InputBox(请输入 b)c = shuixianhua(a, b)End Sub被调函数Public Function shuixianhua(m As Integer, n As Integer) As BooleanDim i As Integer, j As Integer, x As Integer, y As Integer, z As Integer判断 m-n 之间的所有水仙花数For i = m To nshuixianhua = Falsex = i 100 得到百分位的数y = (i - x * 100) 10 得到十位上的数z = i Mod 10 得到各位上的数If x 3 + y 3 + z 3 = i Thenshuixianhua = TrueEnd IfIf shuixianhua ThenPrint i & 是水仙花数End IfNext iEnd Function18、编写一个 Find 函数,能够查找用户输入的数是否在一个随机数组中,如果在数组中, 则把它的位置显示出来。Public Function Find(n As Integer, b() As Integer) As IntegerFor i = LBound(b) To UBound(b)If n = b(i) ThenFind = iEnd IfNext iEnd FunctionPrivate Sub Command1_Click()Dim a(1 To 10) As IntegerDim c As Integer, d As Integerc = Val(InputBox(请输入数字 c)For i = 1 To 10a(i) = iPrint a(i);Next id = Find(c, a()PrintPrint dEnd Sub
展开阅读全文