移红包规则:
①编号为1的堆上的红包,只能移到编号为2的堆上;
②编号为n的堆上的红包,只能移到编号为n-1的堆上;
③其它堆上的红包,可以移到相邻左边或右边的堆上。
现要求找出最少的移动次数使每堆的红包数-样多。如图所示,在文本框Text1中输入红包堆数n,在文本框Text2中依次输入每堆红包的红包个数(以“,”为分隔符和结束符),单击“移红包次数统计”按钮Command1后,在标签Labe13中显示最少的移动次数。
Const Sum = 40 ‘红包总个数
Private Sub Command1_Click()
Dim n As Integer ‘红包的堆数
Dim s As String, k As Integer
Dim avg As Integer '最终每堆的红包个数
Dim ml As Integer, m2 As Integer
Dim tmp As Integer, count As Integer, ans As Integer
n=
s = Text2. Text
avg=Sum/n
tmp=0:count=0:ans=0
For k=1 To Len(s)
c = Mid(s, k, 1)
If c <>”,”Then
tmp =
Else
count = count + 1
If count = 1 Then
m1 =tmp
Else
m2 = tmp
If m1 <> avg Then
m2=m2-(avg-m1)
ans=ans+1
End If
End If
tmp=0
End If
Next k
Labe13. Caption = Str (ans)
End Sub