2012年7月12日 星期四

圍成正方形

這是個有趣的題目,給你已知長度的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. Private Sub Form_Load()
    Dim n(20), l(4) As Integer
    Dim a As Boolean
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n1

    For i = 1 To n1
    Input #1, x
    Sum = 0

    For j = 1 To x
    Input #1, Y
    n(j) = Y
    Sum = Sum + Y
    Next


    For i1 = 1 To 4
    l(i1) = Sum \ 4
    Next

    For j = 1 To x - 1
    For r = j + 1 To x
    If Val(n(j)) < Val(n(r)) Then
    T = n(j)
    n(j) = n(r)
    n(r) = T
    End If
    Next r
    Next j

    For i1 = 1 To 4
    For i2 = 1 To x
    If l(i1) >= n(i2) Then l(i1) = l(i1) - n(i2): n(i2) = 0
    Next
    Next

    a = True

    For i1 = 1 To 4
    If l(i1) <> 0 Then a = False
    Next

    If a = False Then
    Print #2, "0"
    Else
    Print #2, "1"
    End If
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    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 Val(n)
    Line Input #1, x
    a = Split(x): summ = 0
    For j = 1 To UBound(a)
    summ = summ + Val(a(j))
    Next j
    every = summ \ 4: d = 0
    For j = 1 To UBound(a)
    For k = 1 To UBound(a)
    If j <> k Then
    If Val(a(j)) + Val(a(k)) = every Then a(j) = 0: a(k) = 0: d = d + 1
    End If
    Next k
    Next j
    If d = 4 Then Print #2, "1"
    If d <> 4 Then Print #2, "0"
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除