内容发布更新时间 : 2024/12/28 15:55:17星期一 下面是文章的全部内容请认真阅读。
江苏省等级考试二级VB上机试卷2005年(秋)及参考答案
【题目1】本程序的功能是:找出2~10000之间可以表示成22-1形式的素数.本题程序界面见图1.
答: 源程序: Option Explicit
Private Sub Command1_Click()
Dim Exp As Integer, Idx As Integer For Idx = 2 To 10000
If Mersenne(Idx, Exp) Then If Prime(Idx) Then
List1.AddItem Idx & \ End If End If Exp = 0 Next Idx End Sub
Private Function Mersenne(N As Integer, Exp As Integer) As Boolean Dim M As Integer, Ex As Long M = N Ex = 1
Do While M > Ex Exp = Exp + 1 Ex = Ex * 2 Loop
If M = Ex - 1 Then Mersenne = True End If End Function
Private Function Prime(N As Integer) As Boolean Dim K As Integer For K = 2 To Sqr(N)
If N Mod K = 0 Then Exit Function Next K Prime = True End Function
【题目2】将1~9这9个数字顺序排列围成一圈,从中任意一个数字开始,依次将其分成三段,每段数字连在一起算一个数,得到一个2位数、一个3位数和一个4位数,将这三个数相加,使其和数能被77整除.编写一个能找出这三个数的程序. 【编程要求】
1.程序参考界面如图2所示,编程时不得增加或减少界面对象或改变对象的种类,窗体及界面元素大小适中,且均可见;
2.单击“运行”按钮,找出符合条件的三个数,并将它们显示在文本框中;
3.单击“清理”按钮,则将文本框清空,并将焦点设置在“运行”按钮上;单击“结束”按钮,结束程序运行; 4.程序中必须包含一个将数字分段,得到一个2位数、一个3位数和一个4位数的通用过程.
答:源程序:
Private Sub Command1_Click()
Dim s As String, i As Integer, sum As Integer, s1 As Integer, s2 As Integer, s3 As Integer s = \
For i = 1 To Len(s)
Call part(s, i, s1, s2, s3) If (s1 + s2 + s3) Mod 77 = 0 Then
Text1.Text = Text1.Text & \& vbCrLf End If Next i End Sub
Private Sub part(ByVal s As String, i As Integer, s1, s2, s3) s = s & s
s1 = Int(Mid(s, i, 2)) s2 = Int(Mid(s, i + 2, 3)) s3 = Int(Mid(s, i + 5, 4)) End Sub
Private Sub Command2_Click() End End Sub
3