這是個有趣的題目,給你已知長度的n 根棍子,請你試著寫一個程式,把這n 根棍子連
成一個正方形。連接的限制條件如下:
一、棍子只可以用端點來連接
二、不能折斷它
三、每一根棍子都必須使用到
輸入說明:
第一行的數字,代表有幾個問題要求解。第二行開始的每一行,為一個獨立的問題。每
一行的第一個整數為棍子數目n,其範圍為[4, 20] 的整數。接下來的n 個整數,分別代表這
n 根棍子的長度。每根棍子的長度範圍為[1, 100]間的整數。
輸出說明:
對每一個問題以一行輸出,如果所給定的棍子可以連成一個正方形,則輸出1。否則輸
出0。
輸入範例:
2
5 1 3 3 4 5
8 2 5 6 7 1 4 4 3
輸出範例:
0
1
Dim num(), side, test As Integer
回覆刪除Dim allbig As Boolean
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, mycount
For ai = 1 To mycount
Sum = 0: List1.Clear
Input #1, mycount
ReDim num(mycount)
For i = 1 To mycount
Input #1, num(i)
List1.AddItem num(i)
If num(i) > Max Then Max = num(i)
Sum = Sum + num(i)
Next
side = Sum / 4
Do
test = List1.ListCount ^ 2
If Max > side Then Print #2, "0": Exit Do
If Sum Mod 4 <> 0 Then Print #2, "0": Exit Do
For i = 0 To List1.ListCount - 1
If Val(List1.List(i)) = side Then List1.RemoveItem i: Exit For
For ii = 0 To List1.ListCount - 1
If Val(List1.List(ii)) = side - Val(List1.List(i)) And ii <> i Then
If i < ii Then List1.RemoveItem i: List1.RemoveItem ii - 1: Exit For
If i > ii Then List1.RemoveItem i: List1.RemoveItem ii: Exit For
End If
test = test - 1
If test = 0 Then
allbig = True
For bi = 0 To List1.ListCount - 1
For ci = 0 To List1.ListCount - 1
If Val(List1.List(ci)) < side - Val(List1.List(bi)) And ci <> bi Then allbig = False
Next
Next
If allbig = False Then Print #2, "1": Exit Do
If allbig = True Then Print #2, "0": Exit Do
End If
Next
Next
If List1.ListCount = 0 Then Print #2, "1": Exit Do
Loop
Next
Close #2
Close #1
End
End Sub