Sub items()
Dim arr(1 To 10) As Integer
Dim i As Integer, item As Integer,x As Integer
arr(1) = 10
arr(2) = 2
arr(3) = 8
arr(4) = 22
arr(5) = 16
arr(6) = 4
arr(7) = 10
arr(8) = 6
arr(9) = 14
arr(10) = 20
'For i = 1 To 10
' arr(i) = InputBox("请输入第" & i & "孩子分到的糖果数量", "输入窗口", 0)
'Next
item = 0 '经过的次数
Do
If arr(1) = arr(2) And arr(2) = arr(3) And arr(3) = arr(4) _
And arr(4) = arr(5) And arr(5) = arr(6) And arr(6) = arr(7) _
And arr(7) = arr(8) And arr(8) = arr(9) And arr(9) = arr(10) Then
MsgBox "共经过了" & item & "次"
Exit Sub
Else
x = arr(10)
For i = 10 To 2 Step -1
arr(i) = arr(i - 1) / 2 + arr(i) / 2
If Int(arr(i) / 2) <> arr(i) / 2 Then arr(i) = arr(i) + 1
Next
arr(1) = x / 2 + arr(1) / 2
If Int(arr(1) / 2) <> arr(1) / 2 Then arr(1) = arr(1) + 1
item = item + 1
End If
Loop
End Sub
看了前面几个,好象都是用两个数组的,觉得没必要。
[此贴子已经被作者于2005-10-19 15:21:41编辑过] |