用VB6.0写出
用VB6.0写出有17个人围成一个圈(编号0~16)从第0号的人开始从1报数,凡报到3的倍数的人离开圈子,然后再数下去。直到最后只剩下一个人为止。问此人原来的位置号是多少...
用VB6.0写出有17个人围成一个圈(编号0~16)从第0号的人开始从1报数,凡报到3的倍数的人离开圈子,然后再数下去。直到最后只剩下一个人为止。问此人原来的位置号是多少号?
展开
2个回答
展开全部
代码:
Private Sub Command1_Click()
Me.Caption = "Cycle number"
Dim a() As Integer, b() As Integer, n As Integer
n = InputBox("Pleace input number of people:", "Input box", 17)
m = n
ReDim a(1 To n)
For i = 1 To n
a(i) = i - 1
Print "a(" & i & ")=" & a(i) & Space(1);
If i Mod 10 = 0 Then Print
Next i
Print "Count:" & n
If n = 1 Then
Print "Last:"; a(1)
ElseIf n = 2 Then
Print "Last:"; a(2)
Else
Do
k = k + 1
Print "No_" & k & " Round:"
ReDim b(1 To n)
For i = 1 To n
b(i) = a(i)
Next i
For i = 1 To (n Mod 3)
a(i) = b(n - (n Mod 3 - i))
Next i
For i = 1 To (n \ 3)
a(2 * (i - 1) + 1 + n Mod 3) = b(3 * (i - 1) + 1)
a(2 * (i - 1) + 2 + n Mod 3) = b(3 * (i - 1) + 2)
Next i
For i = 1 To 2 * (n \ 3) + n Mod 3
Print "a(" & i & ")=" & a(i) & Space(1);
If i Mod 10 = 0 Then Print
Next i
n = (n \ 3) * 2 + n Mod 3
ReDim Preserve a(1 To n)
Print "Count:" & n
Loop Until n = 2
Print "Last:"; a(2)
Me.Caption = "Number of people=" & m & ",Last:No_" & a(2)
End If
End Sub
Private Sub Command2_Click()
Me.Cls
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Form_Load()
Me.AutoRedraw = True
Me.Caption = "Cycle number"
End Sub
运行效果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询