游戏规则如下:
1)第1轮,将所有灯都关闭
2)第2轮,将所有偶数(编号为2的倍数的灯打开)
3)第3轮,如果编号为3的倍数的灯开着,则关闭,如果灯关闭着,则打开
4)重复游戏直到第n轮结束
现在给定灯的数目n(5<=n<=200),求最后灯关闭的数目。
编写VB程序,实现上述功能,运行程序,在文本框Text1中输入n盏灯数,单击“确定”按钮Command1,在文本框Text2中显示灯灭掉的数量。程序运行界面如、图所示。
请回答下列问题:
Private Sub Command1_Click()
Dim n As Integer, k As Integer, tmp As Integer, num As Integer
Dim a(1 To 200) As Integer, i As Integer, j As Integer
n = Val(Text1.Text)
For i = 1 To n
a(i) = 1
Next i
For i = 1 To n
Do While tmp <= n
If _ Then
a(tmp) = 0
Else
a(tmp) = 1
End If
Loop
Next i
num = 0
For j = 1 To n
If a(j) = 0 Then num = num + 1
Next j
Text2.Text = Str(num)
End Sub