《VB期末范围题总汇》
1. 求随机10个整数的最大值、最小值、平均值以及和;
Dim a(1 To 10)
Private Sub Command1_Click() Randomize
Picture1.Print \"产生的随机数为:\" For i = 1 To 10
a(i) = Int(Rnd * 99 + 1) Picture1.Print a(i); Next i
Picture1.Print End Sub
Private Sub Command2_Click() Dim max, min, ave max = a(1) min = a(1)
ave = 0.1 * a(1) For i = 2 To 10
If a(i) > max Then max = a(i) If a(i) < min Then min = a(i) ave = ave + 0.1 * a(i) Next i
Picture1.Print \"最大数为:\"; max Picture1.Print \"最小数为:\"; min Picture1.Print \"平均数为:\"; ave
End Sub
2.求水仙花数
Private Sub Form_Click()
Dim a, b, c As Integer 'a(个)b(十)c(百)
For a = 0 To 9 For b = 0 To 9 For c = 1 To 9
If a ^ 3 + b ^ 3 + c ^ 3 = a + 10 * b + 100 * c Then MsgBox 100 * c + 10 * b + a End If Next c Next b Next a End Sub
3.百元买百鸡问题;
Option Explicit
Const a = 5, b = 3, c = 1 Private Sub Form_Activate()
Dim i As Integer, j As Integer, k As Integer, n As Integer For i = 1 To 100 For j = 1 To 100 For k = 1 To 100
If i * a + j * b + k * c = 100 Then n = n + 1
List1.AddItem \" 公鸡:\" & i & \" 母鸡:\" & j & \" 小鸡:\" & k End If
Next: Next: Next
MsgBox \"共有组合:\" & n End Sub
求1000以内的所有完数 一个按钮里调用的过程 Private Sub Command1_Click() OutputWanNumber 1000 End Sub
4.'求完数的过程 Private Sub Form_Click() Dim i, j As Integer For i = 4 To 100 temp = 1
For j = 2 To i / 2 If i Mod j = 0 Then temp = temp + j End If Next j
If temp = i Then Print i End If Next i End Sub
5.求各位数字之和 input a:'输入任意数 do
b=a mod 10:'取a的末位数 sum=sum+b:'求和 a=a \\ 10:'去掉末位数
loop until a=0
print \"sum=\";sum:'输出 end
(dim n as string input n
for i = 1 to len(n)
sum=sum+val(mid(n,i,1)) next i
print sum) 6.求最小公倍数
Private Sub Form_Load() Form1.AutoRedraw = True
Dim n1%, m1%, m%, n%, r% n1 = InputBox(\"输入n1\") m1 = InputBox(\"输入m1\")
If m1 > n1 Then '为了求最小公倍数,增加m,n变量 m = m1: n = n1 Else
m = n1: n = m1 End If Do
r = m Mod n
If r = 0 Then Exit Do m = n n = r Loop
Print n1; \的最大公约数为\"; n Print \"最小公倍数=\End Sub
7.求逆序数(感觉题目类型太多)
8. 级数有限项求和问题(题目类型太多)
9. 求质因子问题
Private Sub Command1_Click() Dim N As Integer, I As Integer
N = Val(InputBox(\"请输入2的整数:\")) I = 2 Do
If N Mod I = 0 Then Print I; N = N \\ I
Else I = I 1 End If
Loop While N 1 End Sub
10. 字符统计
Option Base1 Option Explicit
Private Sub Command1_Click()
Dim i as integer,A(26) as integer,n as integer Dim S as string*1,Strl as string Strl=Text1 n=Len(Strl) For i=1 To n S=Mid(Strl,i,1)
If UCase(S)>=”A” And UCase(S)<=”Z” Then A(Asc(UCase(S))-64)+1 End If Next i
For i=1 To 26
List1.Additem Chr(64+i) & “:” & A(i) Netx i End Sub
Private Sub Command_Click() End End Sub 第二大题
1. 判定素数过程
Function isprime(Num As Long) As Boolean If Num < 2 Then isprime = False: Exit Function
Dim i As Long
For i = 2 To Sqr(Num) If (Num Mod i) = 0 Then isprime = False Exit Function End If Next i
isprime = True End Function
Private Sub Command1_Click() Dim i As Long
For i = 1 To 1000 If isprime(i) Then Print i End If Next i
End Sub
2.求最大公约数过程;
Function Max公约数(A As Long, B As Long) '求出两个数的最大公约数 Dim X As Long, Y As Long, K As Long X = IIf(A >= B, A, B) 'x存入最大值 Y = IIf(A <= B, A, B) 'y 存入最小值 Do '辗转相除法
K = X Mod Y: If K = 0 Then Exit Do X = Y: Y = K Loop
Max公约数 = Y End Function
3.冒泡排序过程
Private Sub Command1_Click() Dim a(9) As Integer Dim i As Integer
For i = 0 To 9
a(i) = InputBox(\"输入整数\")
Next Sort a
For i = 0 To 9 Print a(i) Next End Sub
Private Sub Sort(ByRef a() As Integer) Dim i As Integer Dim j As Integer Dim t As Integer
For i = LBound(a) + 1 To UBound(a) For j = UBound(a) To i Step -1
If a(j - 1) > a(j) Then t = a(j - 1) a(j - 1) = a(j) a(j) = t End If Next Next End Sub
4顺序查找过程
Private Sub Command1_Click() Dim i, j, t, a(1 To 10) Randomize Print \"原数组:\"
For i = 1 To 10 a(i) = Rnd * 10
Print \"a(\" & i & \") =\" & a(i) & Space(2), If i Mod 2 = 0 Then Print Next i Print
For i = 1 To 9
For j = i + 1 To 10 If a(j) < a(i) Then t = a(i) a(i
因篇幅问题不能全部显示,请点此查看更多更全内容