小丽在假期看到小朋友们在玩一个游戏,8个小朋友围成一圈,从某个小朋友开始报数,报到指定数时,该小朋友出圈,接着从出圈的位置开始再接着报。她希望通过VB程序模拟这个过程,思路如下:1)用一个一维数组来模拟,编号1~N保存在数据a(1)~a(n)中,来代表N个小朋友。
2)K代表报数的起始位置,从数组的a(k)开始遍列,报到m出圈,将当前位置的元素删除,后面的元素依次往前移位。总数减1。
3)当报数报到数组元素的最后一个时,返回数组开头接着报数。
4)当总数只剩下一个数时,直接输出。
界面如下:
Private Sub Command1_Click( )
Dim i As Integer
Dim n As Integer
Dim m As Integer, k As Integer
Dim A(1 To 50) As Integer '存放编号的数组
n = Val(Text1.Text) '编号个数
k = Val(Text2.Text) '报数起始位
m = Val(Text3.Text) '报到出圈数
If n > 0 And k > 0 And m > 0 ① n <= 50 Then
For i = 1 To n '初始化编号a(1)=1,a(2)=2,……
A(i) = i
Next i
Label1.Caption = ② '调用出圈函数,得到返回的出圈顺序
Else
MsgBox "编号、起始位、报到出圈数都必须大于0!编号不能超过50!"
End If
End Sub
Private Function chuquan(b() As Integer, n As Integer, m As Integer, k As Integer) As String
Dim i As Integer, j As Integer
Dim s As String '出圈顺序存放
i = k - 1
s = "出圈顺序为:"
Do While n > 1
i = i + 1
m = m - 1
If ③ Then
s = s + Str(b(i)) + ","
For j = i To n - 1
b(j) = b(j + 1)
Next j
b(j) = 0
n = n - 1
i = i - 1
m = 4
End If
If i = n Then i = 0
Loop
chuquan = ④
End Function
① {#blank#}1{#/blank#}
② {#blank#}2{#/blank#}
③ {#blank#}3{#/blank#}
④ {#blank#}4{#/blank#}