2011年11月7日 星期一

98正式 Problem7:圍成正方形

Problem7:圍成正方形(17%)
這是個有趣的題目,給你已知長度的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

2 則留言:

  1. Dim s
    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, t
    For i = 1 To t
    Line Input #1, n
    s = Split(n)
    Call A1
    Next
    Close
    Close
    End
    End Sub

    Sub A1()
    Dim sum As Integer
    For i = 1 To s(0)
    sum = sum + Val(s(i))
    Next
    r = sum / 4
    Call A2
    m = sum

    x = 0
    Do
    x = x + 1
    If r >= 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 Sub

    Sub A2()
    For i = 1 To UBound(s)
    For j = 1 To UBound(s) - 1
    If s(j) < s(j + 1) Then
    tmp = s(j)
    s(j) = s(j + 1)
    s(j + 1) = s(j)
    End If
    Next
    Next
    End Sub

    照以前的方做

    回覆刪除
  2. Dim Y() As String
    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 ii = 1 To N
    Line Input #1, X
    Y = Split(X)
    total = 0

    For i = 0 To UBound(Y)
    total = total + Y(i)
    Next i

    average = total / 4

    For i = 0 To UBound(Y) - 1
    For j = i + 1 To UBound(Y)
    If Y(i) < Y(j) Then
    T = Y(i)
    Y(i) = Y(j)
    Y(j) = T
    End If
    Next j
    Next i

    For j = 1 To 4
    Z = average
    For i = 0 To UBound(Y)
    If Z >= Val(Y(i)) Then Z = Z - Val(Y(i)): Y(i) = 0
    Next i
    If j = 4 Then If OK = True Then Print #2, 1 Else Print #2, 0
    Next j

    Next ii
    Close #2
    Close #1
    End
    End Sub


    Function OK() As Boolean
    P = True
    For i = 0 To UBound(Y)
    If Y(i) <> 0 Then P = fasle
    Next i
    OK = P
    End Function

    回覆刪除