题型:综合题 题类:常考题 难易度:困难
浙江省高中信息技术 数组练习
移牌规则:在编号为①的堆上取的纸牌,只能移到编号为②的堆上;在编号为N(N≥2)的堆上取的纸牌,只能移到编号为N-1的堆上;其他堆上取的纸牌,可以移到相邻左边或右边的堆上。
现在要求找出一种移动方法,用最少的移动次数使每堆上纸牌数都一样多。
例如N=4,4堆纸牌数分别为:
① |
② |
③ |
④ |
9 |
8 |
17 |
6 |
移动3次可达到目的:从③取4张牌放到④(9 8 13 10)→从③取3张牌放到②(9 11 10 10)→从②取1张牌放到①(10 10 10 10)。
现编写如下代码解决4堆任意张数牌的移动问题,请按提示补充划线句的代码:
Private Sub Command1_Click()
Dim aver As Single
Dim st As Integer ‘统计移动的步数
Dim ssum, i, j As Integer
Dim a( 1 To 100) As Integer
a(1) = Val(Text1.Text) ‘读取各堆牌张数,求总张数aver
a(2) = Val(Text2.Text)
a(3) = Val(Text3.Text)
a(4) = Val(Text4.Text)
ssum =a(1) + a(2) + a(3) + a(4)
aver=ssum \ 4 ‘求牌的平均张数aver
For i=1 To 4
①
Next i
i=1 ‘过滤初端和末端的0
j = 4
Do While a(i) = 0 And i < 4
i=i+1
Loop
Do While a(j) = 0 And j > 1
j = j-1
Loop
st = 0
Do While i < j ‘开始移动牌使数组a元素值均为0并过滤掉
②
a(i) = 0
st = st + 1
i=i+1
Do While a(i) = 0 And i<j ‘过滤掉数组a元素值均为0
③
Loop
Loop
Print st ‘输出移动次数
End Sub
试题篮