题库 高中信息

题干

素数只能被 1 和它本身整除,不能被其他自然数整除。编写 VB 程序实现如下功能:单击“产生奇数并判断”按钮 Command1,随机产生一个三位正奇数显示在文本框 Text1 中,并在文本框 Text2 中显示其是否为素数的判断结果。例如,当随机产生的三位正奇数为 953 时,程序运行界面如图 a 所示。

(1)在设计程序界面时,应使用图 b 所示“控件工具箱”中的_____(填写相应编号)添加文本框 Text1。
(2)实现上述功能的 VB 程序如下,请在划线处填写合适的代码。
Private Sub Command1_Click()
Dim n As Integer, i As Integer
Dim flag As Boolean ‘用于标记是否为素数
Randomize
n =____________________ ‘n 为三位正奇数
Text1.Text = Str(n) : flag = True: i = 3
Do While i <= n - 1 And flag = True
If n Mod i = 0    Then flag = False
End If
i = i + 2
Loop
If ____________Then
Text2.Text = Str(n) + “是素数”
Else
Text2.Text = Str(n) + “不是素数”
End If
End Sub
(3)以下选项中,与加框处表达式“n Mod i = 0”等价的是_____(单选,填字母)。
A.    n \ i = Int(n / i)B.n \ i = n/ iC.n Mod i = n \ i
上一题 下一题 0.99难度 填空题 更新时间:2019-11-27 12:16:25

答案(点此获取答案解析)

同类题2

(加试题)异或的数学符号为“⊕”,其运算法则相当于不带进位的二进制加法:0⊕0=0,1⊕0=1,0⊕1=1,1⊕1=0(即符号两侧数值相同时,计算结果为0;数值不同时为1)。
如果要对两个十进制数进行异或运算,可以按以下步骤进行:
①   先将要进行异或运算的两个十进制数分别转换为二进制;
②   对两个二进制数按位进行异或运算;例:(101101)2⊕(111)2=(101010)2
③   再把步骤②中的运算结果转换为十进制,该十进制数即为运算结果。
小明编写了一个VB程序来模拟上述运算过程,程序功能如下:在文本框Text1和Text2中分别输入要参加异或运算的十进制数,单击计算按钮Command1,程序对输入的两个数进行异或运算,并将运算结果显示在文本框Text3中,程序运行界面如图所示。

(1)通过以上关于异或运算的描述,可知10⊕6的结果是___________。
(2)实现上述功能的VB程序如下。请在划线处填入合适的代码。
Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer
Dim a1 As String, b1 As String
Dim lena1 As Integer, lenb1 As Integer, i As Integer
Dim result As String
a = Val(Text1.Text)
b = Val(Text2.Text)
If a > b Then
c = a: a = b: b = c
End If
result = ""
a1 = DtoB(a): b1 = DtoB(b)
lena1 = Len(a1): lenb1 = Len(b1)
i = 1
Do While i <= lena1
If Mid(a1, lena1 - i + 1, 1) = Mid(b1, lenb1 - i + 1, 1) Then
result = "0" + result
Else
result = "1" + result
End If
i = i + 1
Loop
result =_____
Text3.Text = BtoD(result)
End Sub
Public Function DtoB(x As Integer) As String
Dim remainder As String
DtoB = ""
Do While x > 0
remainder = CStr(x Mod 2)  '如:CStr(3 Mod 2)的值为"1"
DtoB = remainder + DtoB
________  
Loop
End Function
Public Function BtoD(x As String) As Integer
Dim i As Integer
BtoD = 0
For i = 1 To Len(x)
BtoD =________+ Val(Mid(x, i, 1))
Next i
End Function