⑴枚举两根短的木棒a和b;
⑵当a <> b 时,组合的数量= 长度为a 的木棒根数×长度为b 的木棒根数×长度为(a+b)的木棒取两根的取法数量;
⑶当a = b 时,组合的数量= 长度为a 的木棒取两根的取法数量×长度为(a+b)的木棒取两根的取法数量。
如木棒长度分别为“1,1,2,2,3,3”,当a=1,b=2 时,则c=d= 3,该情况的组合数量=(长度为1 的根数)*(长度为2 的根数)*(长度为3 的根数取两根的取法数量)= 2 *2 * 1 = 4。长度为3 的根数取两根的取法数量,即
程序运行界面如图所示,在文本框Text1输入一堆木棒的长度,以逗号间隔,点击“计算”按钮Command1后在标签Label1显示方案总数。
请回答下列问题:
Private Sub Command1_Click( )
Dim tmax As Integer, ans As Integer, k As Integer
Dim i As Integer, a As Integer, b As Integer, tmp As Integer
Dim c As String, s As String
Dim t(20) As Integer
'储存相同长度木棒的数量,如t(5)的值是长度为5的木棒的数量
s = Text1.Text
tmp = 0: k = 0
ans = 0: tmax = 0
For i = 1 To Len(s)
c = Mid(s, i, 1)
If c = "," Then
k = k + 1
t(tmp) =
If tmp > tmax Then tmax = tmp
tmp = 0
Else
tmp = tmp * 10 + Val(c)
End If
Next i
For a = 1 To tmax \ 2 '枚举两根短的木棒
For b = a To tmax
If a + b > tmax Or t(a) * t(b) * t(a + b) = 0 Then Exit For
If Then
ans = ans + js(t(a), 2) * js(t(a + b), 2)
Else
ans = ans + js(t(a), 1) * * js(t(a + b), 2)
End If
Next b
Next a
Label1.Caption = "共有" + Str(ans) + "种方案"
End Sub
Function js(x As Integer, y As Integer) As Integer
'计算在x个对象中取y个对象的组合数量,即
'代码略。
End Function