呃,本来觉得是挺简单的事情,后来想复杂了
扩展开去,是一个很难的问题,时间问题,最后只能用取巧的方法处理....
如果列举所有的可能性,耗时耗力,偷懒用随机的方式做,多运行几次就能得到答案了
红色代码自己改
'随机叠箱子
Dim cm33 As Integer = 9
Dim cm22 As Integer = 5
Dim arys(cm33+cm22-1) As Integer
For i As Integer = 0 To cm33-1
arys(i) = 33
Next
For i As Integer = cm33 To cm33+cm22-1
arys(i) = 22
Next
Dim min As Integer = 99999
Dim answer As String = ""
For i As Integer = 1 To 100 '随机100次结果
Dim sum(3) As Integer
For j As Integer = 1 To 20 '打乱数组20次
Dim r1 As Integer = Rand.Next(arys.length-1)
Dim r2 As Integer = Rand.Next(arys.length-1)
Dim temp As Integer = arys(r1)
arys(r1) = arys(r2)
arys(r2) = temp
Next
Dim str As String = ""
For k As Integer = 0 To arys.length - 1
Dim idx As Integer = k Mod 4
sum(idx) += arys(k)
str &= arys(k) & ","
Next
Dim max As Integer = sum(0)
For k As Integer = 1 To sum.length - 1
If sum(k) > max Then max = sum(k)
Next
Dim diff As Integer = max-sum(0)+max-sum(1)+max-sum(2)+max-sum(3)
output.show(str.trim(",") & " 各边:" & cstr(sum(0)) & " " & sum(1) & " " & sum(2) & " " & sum(3) & " 相差:" & diff)
If diff < min Then
min = diff
answer = str.trim(",") & " 各边:" & cstr(sum(0)) & " " & sum(1) & " " & sum(2) & " " & sum(3) & " 相差:" & diff
End If
Next
msgbox(answer)