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 \输出 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; \的最大公约数为\ 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 \ 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
编程题(20分,正式试题为1题)
1、 编写一个身份证号码转换程序: (1) 老身份证是15位,先要增加两位年份,变成17位,再计算校验位。
(2) 校验位的计算方法是:17位身份证号码Number每一位都乘上一个权值Weight,然
后相加,再除以11,取其余数作为位置数Position。 (3) 根据位置数Position查表,得到校验位CheckCode。 (4) Number[i]: 表示身份证号码第 i 位上的号码
Weight[i]: 表示第 i 位上的权值
Weight :7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1 Position = ∑(Number[i]*Weight[i]) mod 11 (5) Position :0 1 2 3 4 5 6 7 8 9 10 CheckCode:1 0 X 9 8 7 6 5 4 3 2
其中罗马数字X表示10,所以在新标准的身份证号码中可能含有非数字的字母X。
2、 利用递归函数编写打印杨辉三角形 (参见教材第319页上的实验6.11) (1) 编写求Cn,m的递归函数,其递归公式为:Cn,m=Cn,m-1+Cn-1,m-1 (2) 递归条件是:C0,m=1 当n = 0
C1,m= m 当n = 1
Cn,m= Cm-n,m
C0,0
C1,0 C1,1
当n > m/2
(3) 利用上述递归函数编写打印杨辉三角形的程序
C2,0 C2,1 C2,2 · · · · · · ·
Cn,0 Cn,1 · · · · · · · Cn,n-1 Cn,n