這是個有趣的題目,給你已知長度的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
想了一個下午:D
回覆刪除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, N
For i = 1 To N
Line Input #1, X
Y = Split(X)
total = 0
For j = 1 To Y(0)
total = total + Y(j)
Next j
If total Mod 4 = 0 Then
M = total / 4
z = 0
Do
z = z + 1
If M >= Val(Y(z)) Then total = total - Y(z)
Loop Until total = 0 Or z = Val(Y(0))
End If
If total = 0 Then Print #2, "1" Else Print #2, "0"
Next i
Close #2
Close #1
End
End Sub
in.txt--------------------
4
5 1 3 3 4 5
8 2 5 6 7 1 4 4 3
4 1 1 1 2
5 4 4 4 3 1
out.txt-------------------
0
1
0
1
[4,20] 這是指 4 到 20 間的數嗎 ?
回覆刪除題目有點不詳細
對阿4<= <=20
回覆刪除他歷屆試題都這樣表示@@"
題本還是網路找的 ??
回覆刪除佑好,
回覆刪除你只算了一種,如果被4整除,如果每根都不會超過一邊的長,你就說這樣可以,真的嗎?
來試試好了,要組成一個每邊4的正方形,共要16。如果用5根3和1根1,可以變成正方形嗎?
再加油。
用邊長去取
回覆刪除Private Sub Form_Load()
Dim M(4) As Integer
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, n
For i = 1 To n
Line Input #1, X
Y = Split(X)
total = 0
For j = 1 To Val(Y(0))
total = total + Val(Y(j))
Next j
If total Mod 4 = 0 Then
For A = 1 To 4
M(A) = total / 4
Next A
For j = 1 To Val(Y(0)) - 1
For r = j + 1 To Val(Y(0))
If Val(Y(j)) < Val(Y(r)) Then
T = Y(j)
Y(j) = Y(r)
Y(r) = T
End If
Next r
Next j
For j = 1 To 4
For r = 1 To Val(Y(0))
If M(j) >= Val(Y(r)) Then M(j) = M(j) - Val(Y(r)): Y(r) = 0
Next r
Next j
End If
P = False
For j = 1 To 4
If M(j) <> 0 Then P = True
Next j
If P = False Then Print #2, 1 Else Print #2, 0
Next i
Close #2
Close #1
End
End Sub
佑好,
回覆刪除設總長40,每邊10,
由大到小排每段為「8,7,6,5,4,3,2,2,2,2,1」,可以。
設總長44,每邊11,
由大到小排每段為「9,8,7,7,4,3,3,2,1」,也可以。
嗯,你的程式正確。
Dim ss, s, F, AANS
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Dim sum
Line Input #1, ti
For i = 1 To ti
ans = 0
Line Input #1, a
sum = 0
s = Split(a)
ss = Split(a)
'Lv1
For j = 1 To UBound(s)
sum = Val(s(j)) + sum
Next
If sum Mod 4 = 0 Then
'Lv2
F = sum / 4
Call AA
x = 0
m = F * 4
Do
x = x + 1
If F >= Val(s(x)) Then m = m - s(x)
Loop Until m = 0 Or x = Val(s(0))
If m = 0 Then Print #2, "1" Else Print #2, "0"
End If
Next
Close
Close
End Sub
Sub mysub()
End Sub
Function re(a)
Dim ch As Boolean
For i = 0 To List1.ListCount - 1
If a = List1.List(i) Then ch = True
Next
re = ch
End Function
' 1 1 2 2 3 4 5 6
Sub AA()
For j = 1 To UBound(s)
For i = 1 To UBound(s) - 1
If s(i) > s(i + 1) Then
tmp = s(i)
s(i) = s(i + 1)
s(i + 1) = tmp
End If
Next
Next
End Sub
根據佑的方法
我一直都想從正面去解
沒想到反面比較簡單
arro好,
回覆刪除程式正確。
想解題方式,也是考試決勝點。