Dim a(100) As Integer For i = 1 To 100 a(i) = Int(Rnd * (999 - 100 + 1)) + 100 Next For i = 1 To 100 For j = 1 To 100 - i If a(j) < a(j + 1) Then t = a(j) a(j) = a(j + 1) a(j + 1) = t End If Next Next For i = 1 To 100 If a(i) Mod 3 = 0 Then Print a(i); Form1.ForeColor = vbRed Else Print a(i); Form5.ForeColor = vbBlack End If If i Mod 5 = 0 Then Print End If Next
Print "二维数组为:" Dim a(5, 5) For i = 1 To 5 For j = 1 To 5 a(i, j) = Int(Rnd * (99 - 10 + 1)) + 10 Print a(i, j); Next Print Next Print Print "最外层的和:" For i = 1 To 5 For j = 1 To 5 If i = 1 Or j = 1 Or i = 5 Or j = 5 Then s = s + a(i, j) If a(5, 5) = a(i, j) Then Sum = Sum & a(i, j) & "=" & s Else Sum = Sum & a(i, j) & "+" End If End If Next Next Print Sum
155.杨辉三角
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Dim a(10, 10) For i = 1 To 10 a(i, i) = 1 a(i, 1) = 1 Next For i = 1 To 10 For j = 1 To i - 1 a(i, j) = a(i - 1, j - 1) + a(i - 1, j) Next Next For i = 1 To 10 For j = 1 To 10 Print a(i, j); Next Print Next
Dim a(), l, n, item As Integer Private Sub Form_Load() a = Array(5, 15, 23, 52, 68, 88, 90, 99) End Sub Private Sub Command1_Click() Cls item = Val(InputBox("请输入:")) n = n + 1 l = UBound(a) + n ReDim Preserve a(l) a(l) = item For i = 1 To UBound(a) For j = 0 To UBound(a) - i If a(j) > a(j + 1) Then t = a(j) a(j) = a(j + 1) a(j + 1) = t End If Next Next For i = 0 To UBound(a) Print a(i); Next End Sub
Dim a(8, 9) Me.FontSize = 15 Print "第十四届二中大学全校师生肃反协会第四次会议" Print " 分数1 分数2 分数3 分数4 分数5 分数6 分数7 总分 是否票决" For i = 1 To 8 s = 0 For j = 1 To 7 a(i, j) = Int(Rnd * (100 - 1 + 1)) + 1 s = s + a(i, j) Sum = Sum + s a(i, 8) = s Next Next For i = 1 To 8 Max = a(i, 8) For j = i + 1 To 8 If a(j, 8) > Max Then For t = 1 To 8 Max = a(j, t) a(j, t) = a(i, t) a(i, t) = Max Next End If Next Next For i = 1 To 8 Print "代表" & i; For j = 1 To 9 Print Format(a(i, j), "@@@@@@@"); If a(i, 8) < 400 Then a(i, 9) = "是" Else a(i, 9) = "否" End If Next Print Next Print "计票总分:" & Sum & " 总分平均率:" & Sum / UBound(a) - LBound(a) - 1
Private Sub Command1_Click() Dim a, b, c, p, sum As Double a = CDbl(Text1.Text) b = CDbl(Text2.Text) c = CDbl(Text3.Text) If a + b > c Or a + c > b Or c + b > a Then p = (a + b + c) / 2 pa = p - a pb = p - b pc = p - c sum = Round(Sqr(p * (pa * pb * pc)), 5) Text4.Text = sum Text4.Enabled = True Else MsgBox "不能组成三角形!" End If End Sub Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" Text1.SetFocus Text4.Enabled = False sum = 0 End Sub
Dim i, n As Integer Private Sub Command1_Click() Timer1.Enabled = True Label1 = n \ 60 Mod 60 & "分" & n Mod 60 & "秒" End Sub Private Sub Option1_Click(Index As Integer) Select Case Index Case 0 n = 60 Case 1 n = 300 Case 2 n = 600 End Select End Sub Private Sub Timer1_Timer() Cls If n = 0 Then MsgBox "时间到!" Else n = n - 1 Label1 = n \ 60 Mod 60 & "分" & n Mod 60 & "秒" End If End Sub
160.同构数
1 2 3 4 5 6 7 8 9 10 11
Dim n, a, b, c, d, e, f As Integer For i = 1 To 1000 n = i ^ 2 a = n Mod 10 b = n Mod 100 c = n Mod 1000 d = n Mod 10000 If i = a Or i = b Or i = c Or i = d Then Print i, n End If Next
参考答案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
s = 0 For i = 1 To 1000 res = i ^ 2 t = 10 Do While res \ t > 0 j = res Mod t t = t * 10 Print j, t If j = i Then Exit Do End If Loop If j = i Then Print i End If Next
Private Sub Combo5_Click() If Combo5.ListIndex = 0 Then Text1.Alignment = 0 If Combo5.ListIndex = 1 Then Text1.Alignment = 1 If Combo5.ListIndex = 2 Then Text1.Alignment = 2 End Sub Private Sub Combo6_Click() If Combo6.ListIndex = 0 Then Text1.FontItalic = True Else Text1.FontItalic = False End If If Combo6.ListIndex = 1 Then Text1.FontBold = True Else Text1.FontBold = False End If If Combo6.ListIndex = 2 Then Text1.FontUnderline = True Else Text1.FontUnderline = False End If End Sub Private Sub Combo7_Click() For i = 0 To 6 If Combo7.ListIndex = i Then Text1.FontSize = i * 10 + 10 Next End Sub Private Sub Combo8_Click() If Combo8.ListIndex = 0 Then Text1.Font = "黑体" If Combo8.ListIndex = 1 Then Text1.Font = "宋体" If Combo8.ListIndex = 2 Then Text1.Font = "仿宋" If Combo8.ListIndex = 3 Then Text1.Font = "微软雅黑" End Sub Private Sub Combo1_Click() If Combo1.ListIndex = 0 Then Text1.ForeColor = vbBlack If Combo1.ListIndex = 1 Then Text1.ForeColor = vbWhite If Combo1.ListIndex = 2 Then Text1.ForeColor = vbRed If Combo1.ListIndex = 3 Then Text1.ForeColor = vbYellow If Combo1.ListIndex = 4 Then Text1.ForeColor = vbBlue End Sub Private Sub Combo2_Click() If Combo2.ListIndex = 0 Then Text1.BackColor = vbBlack If Combo2.ListIndex = 1 Then Text1.BackColor = vbWhite If Combo2.ListIndex = 2 Then Text1.BackColor = vbRed If Combo2.ListIndex = 3 Then Text1.BackColor = vbYellow If Combo2.ListIndex = 4 Then Text1.BackColor = vbBlue End Sub Private Sub Form_Load() Combo5.AddItem "左对齐": Combo5.AddItem "右对齐": Combo5.AddItem "居中" Combo6.AddItem "斜体": Combo6.AddItem "粗体": Combo6.AddItem "下划线" Combo7.AddItem "10": Combo7.AddItem "20": Combo7.AddItem "30" Combo7.AddItem "40": Combo7.AddItem "50": Combo7.AddItem "60" Combo7.AddItem "70": Combo8.AddItem "黑体": Combo8.AddItem "宋体" Combo8.AddItem "仿宋": Combo8.AddItem "微软雅黑": Combo1.AddItem "黑色" Combo1.AddItem "白色": Combo1.AddItem "红色": Combo1.AddItem "黄色" Combo1.AddItem "蓝色": Combo2.AddItem "黑色": Combo2.AddItem "白色" Combo2.AddItem "红色": Combo2.AddItem "黄色": Combo2.AddItem "蓝色" End Sub
Dim a(), n, key, item As Integer, flag As Boolean Private Sub Form_Click() Cls ReDim a(0 To 9) Print "原来数组为:" For i = 0 To UBound(a) a(i) = Int(Rnd * (99 - 10 + 1)) + 10 Print a(i); Next Print End Sub Private Sub Command1_Click() item = Val(InputBox("查找的数是:")) flag = False For i = 0 To UBound(a) If item = a(i) Then key = i flag = True End If Next If flag = True Then For i = key To UBound(a) - 1 a(i) = a(i + 1) Next Print: n = n + 1 Print "删除" & item & "后的数组是:" For i = 0 To UBound(a) - n Print a(i); Next Else Print Print item & "不在该数组中" End If End Sub
Dim i, m, h, s, flag As Integer Private Sub Command1_Click() flag = flag + 1 i = 0 If flag Mod 2 = 0 Then Command1.Caption = "开始计时" Timer1.Enabled = False h = Val(Label1) \ 3600 m = (Val(Label1) Mod 3600) \ 60 s = Val(Label1) Mod 60 MsgBox "一共运行了" & h & "小时" & m & "分" & s & "秒" Label1 = 0 Else Command1.Caption = "停止" Timer1.Enabled = True Label1 = 0 End If End Sub Private Sub Timer1_Timer() i = i + 1 Label1 = i End Sub
164.判断回文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Dim flag As Boolean a = Text1 flag = False For i = 1 To Len(a) / 2 res = Mid(a, 1, Len(a) \ 2) ser = StrReverse(Right(a, Len(a) \ 2)) If Mid(res, i, 1) = Mid(ser, i, 1) Then flag = True Else flag = False Exit For End If Next If flag = True Then Print a & "是回文" Else Print a & "不是回文" End If
165.换零钱
1 2 3 4 5 6 7 8 9 10
a = 0: b = 1 For i = 1 To 30 s = a + b b = a If s > 1000 Then Exit For a = s sum = sum + s Print s Next Print "数列和:" & sum
Text1 = "": Text2 = "": Text3 = "" Dim a%(1 To 20), b!(1 To 20), sum!(1 To 20) For i = 1 To 20 a(i) = Int(Rnd * (10 - 0 + 1)) + 0 If i Mod 5 <> 0 Then Text1 = Text1 & a(i) & " " Else Text1 = Text1 & vbCrLf End If Next For i = 1 To 20 b(i) = Int(Rnd * (10 - 0 + 1)) + 0 If i Mod 5 <> 0 Then Text2 = Text2 & b(i) & " " Else Text2 = Text2 & vbCrLf End If Next For i = 1 To 20 sum(i) = a(i) + b(i) If i Mod 5 <> 0 Then Text3 = Text3 & sum(i) & " " Else Text3 = Text3 & vbCrLf End If Next
Dim a(1 To 10) For i = 1 To 10 a(i) = Int(Rnd * (99 - 10 + 1)) + 10 Print a(i); Next For i = 1 To 10 Key = i For j = i + 1 To 10 If a(j) < a(Key) Then Key = j End If Next If Key <> i Then t = a(i) a(i) = a(Key) a(Key) = t End If Next: Print For i = 1 To 10 Print a(i); Next
Text2 = "": Text3 = "" a = Text1 n = 5 For i = 1 To Len(a) res = Mid(a, i, 1) If res >= "a" And res <= "z" Then t = Asc(res) + n If t > Asc("z") Then t = t - 26 End If ElseIf res >= "A" And res <= "Z" Then t = Asc(res) + n If t > Asc("Z") Then t = t - 26 End If End If Text2 = Text2 & Chr(t) Next a = Text2 For i = 1 To Len(a) res = Mid(a, i, 1) If res >= "a" And res <= "z" Then t = Asc(res) - n If t > Asc("z") Then t = t + 26 End If ElseIf res >= "A" And res <= "Z" Then t = Asc(res) + n If t > Asc("Z") Then t = t + 26 End If End If Text3 = Text3 & Chr(t) Next
169.字符串处理
说明:统计英文单词的个数,如果以“ ”,“?”,“!”,“.”,“,”,“;”结尾视为一个单词
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Dim flag As Boolean, n As Integer, res As String flag = False For i = 1 To Len(Text1) res = Mid(Text1, i, 1) If res = " " Or res = "?" Or res = "!" Or res = "." Or res = "," Or res = ";" Then flag = False Else If flag = False Then n = n + 1 flag = True End If End If Next Print "单词数量为:" & n
Dim key As Integer key = 100 Do While key <> 1 If key Mod 2 = 0 Then Print key & "/2=" & key / 2 key = key / 2 Else Print Trim(key); key = key * 3 + 1 Print "*3+1=" & Trim(key); Print End If Loop
171.四方定律
说明:四方定律即自然数最多只要用4个数的平方和就可以求出
1 2 3 4 5 6 7 8 9 10 11 12 13
t = 2333 For a = 1 To t For b = 1 To a For c = 1 To b For d = 1 To c If a ^ 2 + b ^ 2 + c ^ 2 + d ^ 2 = t Then Print a & "^2+"; b & "^2+" & c & "^2+" & d & "^2=" & t Exit Sub End If Next Next Next Next
Dim flag As Boolean n = 3 flag = True For i = 1 To n - 1 s = 0 If i Mod 2 = 0 Then s = s + 1 End If If s > 0 Then flag = True Else flag = False End If Print s Next If flag Then Print "是质数" Else Print "不是质数" End If
173.求数组最大值
如图求出数组每行的最大值,并标出它的坐标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Dim a(5, 5) For i = 1 To 5 Max = a(1, 1) For j = 1 To 5 a(i, j) = Int(Rnd * (99 - 10 + 1)) + 10 If a(i, j) > Max Then Max = a(i, j) Key = j x = i y = j End If Print a(i, j); Next Print "Max: " & a(i, Key) & " a(" & x & "," & y & ")"; Print Next
174.打印矩阵
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Dim a(5, 5) As Integer Dim str As String Private Sub Form_Click() Cls For i = 1 To 5: n = n + 1: a(i, 1) = n: Next For i = 2 To 5: n = n + 1: a(5, i) = n: Next For i = 4 To 1 Step -1: n = n + 1: a(i, 5) = n: Next For i = 4 To 2 Step -1: n = n + 1: a(1, i) = n: Next For i = 2 To 4: n = n + 1: a(i, 2) = n: Next For i = 3 To 4: n = n + 1: a(4, i) = n: Next For i = 3 To 2 Step -1: n = n + 1: a(i, 4) = n: Next For i = 2 To 3: n = n + 1: a(i, 3) = n: Next For i = 1 To 5 For j = 1 To 5 Print Format(a(i, j), "@@@@"); Next Print Next End Sub
Private Sub Form_Click() Cls Dim a(0 To 10) For i = 0 To 10 a(i) = Int(Rnd * (99 - 10 + 1)) + 10 Print a(i); Next Print QuickSort a(), LBound(a), UBound(a) Print For i = 0 To 10 Print a(i); Next End Sub Sub QuickSort(MyArray(), L, R) '获取数组,并取得下、上界值到L、R Dim i, j, pivot, y i = L '确定从数组左边与轴比较的元素位置I j = R '确定从数组右边与轴比较的元素位置J pivot = MyArray((L + R) / 2) '将X选取为数组的轴 While (i <= j) '当左右查询位置没有相交时执行 While (MyArray(i) < pivot And i < R) '在左边找出大于 X 的值 i = i + 1 Wend While (pivot < MyArray(j) And j > L) '在右边找出小于 X 的值 j = j - 1 Wend If (i <= j) Then '当查询的位置没有相交时执行 y = MyArray(i) '将小于轴的元素放在左边,大于轴的换到右边 MyArray(i) = MyArray(j) MyArray(j) = y i = i + 1 '移动从左边开始的查询位置 j = j - 1 '移动从右边开始的查询位置 End If For t = 0 To 10 Print MyArray(t); Next Print Wend '递归过程 If (L < j) Then Call QuickSort(MyArray(), L, j) '分析轴左边的数组 If (i < R) Then Call QuickSort(MyArray(), i, R) '分析轴右边的数组 End Sub
Private Sub Form_Click() Cls Dim key% key = 50 Print "验证哥德巴赫猜想" & key & "以内的真偶数是成立的" For i = 2 To 50 Step 2 For j = 2 To key \ 2 t = key - j t1 = prime(j) If t1 = True Then t2 = prime(t) If t2 = True Then Print Format(i & "=" & t & "+" & j, "@@@@@@@@@@@"); n = n + 1 If n Mod 15 = 0 Then Print End If End If End If Next Next End Sub Private Function prime(n) As Boolean prime = True For i = 2 To n - 1 If n Mod i = 0 Then prime = False Else prime = True End If Next End Function
177.数字金字塔
打印金字塔
1 2 3 4 5 6 7 8 9 10
For i = 1 To 9 Print Space(30 - 3 * i); For j = 1 To i Print j; Next For j = i - 1 To 1 Step -1 Print j; Next Print Next
Dim a(4, 5): Cls For i = 1 To 4 For j = 1 To 5 a(i, j) = Int(Rnd * (99 - 10 + 1)) + 10 Print a(i, j); Next Print Next: Print: Print For i = 1 To 5 t = a(1, i) a(1, i) = a(3, i) a(3, i) = t Next For i = 1 To 4 t = a(i, 2) a(i, 2) = a(i, 4) a(i, 4) = t Next For i = 1 To 4 For j = 1 To 5 Print a(i, j); Next Print Next
Dim a(1 To 100) As Integer Private Sub Command1_Click() Cls Print "排序前:" For i = 1 To 100 a(i) = Int(Rnd * (1000 - 1 + 1)) + 1 Print a(i); If i Mod 10 = 0 Then Print Next For i = 1 To 100 For j = 1 To 100 - i If a(j) > a(j + 1) Then t = a(j): a(j) = a(j + 1): a(j + 1) = t Next Next Print "排序后:" For i = 1 To 100 Print i & ":" & Format(a(i), "@@@") & Space(2); If i Mod 10 = 0 Then Print Next End Sub Private Sub Command2_Click() Print Dim search, mid, guess As Integer Dim low, high As Integer Static n As Integer n = n + 1 search = Val(InputBox("请输入要查询的数字:", "第" & n & "次")) low = 0 high = UBound(a) - 1 Do While low <= high mid = (low + high) guess = a(mid) If guess = search Then Print search & "在" & mid & "处" Exit Sub End If If guess > search Then high = mid - 1 Else low = mid + 1 End If Loop End Sub
For a = 0 To 100 For b = 0 To 100 For c = 0 To 100 If (5 * a + (3 * b) + c / 3) = 100 And _ c Mod 3 = 0 And _ a + b + c = 100 Then n = n + 1 Print "#" & n & " : VB : " & a _ & " CPT : " & b & " : " _ & " SER : " & c End If Next Next Next ' 二种 For a = 0 To 33 For b = 0 To 20 c = 100 - a - b If 5 * b + 3 * a + c / 3 = 100 Then n = n + 1 Print "#" & n & " : VB : " & b _ & " CPT : " & a _ & " : " & " SER : " & c End If Next Next
182.简单计算题
1 2 3 4 5 6 7 8 9
Dim f, s As Double n = 1 num = 10 For i = 1 To num - 1 f = f * -(1 ^ 2) + (1 / (i + 3)) Next s = 1 - f Print s Print Int(s * 1000) / 1000
183.格利高里公式
1 2 3 4 5 6 7
s = 0 a = 1 For i = 1 To 99 Step 2 s = s + 1 / i * a a = -a Next Print Round(s * 4, 2)
184.趣味数学题
185.打印题
1 2 3 4 5 6 7 8 9 10
n = 20 Form1.FontSize = 25 For i = 1 To n If i = 1 Then Print Space(n - 1) & "*"; If i > 1 And i <= n - 1 Then Print Space(n - i) & "*" & Space(i - 2) & "*" & Space(i - 2) & "*"; End If If i = n Then Print String(i * 2 - 1, "*"); Print Next
Private Sub Command1_Click() Select Case Combo1.ListIndex Case 0 Text3 = Val(Text1) + Val(Text2) Case 1 Text3 = Val(Text1) - Val(Text2) Case 2 Text3 = Val(Text1) * Val(Text2) Case 3 If Val(Text2) = 0 Then MsgBox "0不能作为除数!", vbExclamation, "错误" Exit Sub Else Text3 = Val(Text1) / Val(Text2) End If End Select End Sub Private Sub Form_Load() Combo1.AddItem "+" Combo1.AddItem "-" Combo1.AddItem "*" Combo1.AddItem "/" Combo1.Text = Combo1.List(0) End Sub
191.整数对换
1 2 3 4 5 6
n = 56 For i = 10 To 99 If n + i = Val(StrReverse(n)) + Val(StrReverse(i)) Then Print n & "+(" & i; ")=(" & Val(StrReverse(i)) & ")+" & Val(StrReverse(n)) End If Next
192.打印
1 2 3 4 5 6 7
For i = 0 To 4 For j = 0 To i t = i * 5 + j Print t; Next Print Next