2012年11月5日 星期一

方塊積木

13歲的小明喜歡玩他的方塊積木,他總是把方塊疊在一起,型成高度不一的方塊堆。然後他說:這是一面牆。五歲的妹妹小每聽到了就跟小明說:真正的牆高度應該要一樣才行。小明聽了覺得有道理,於是決定要搬動一些方塊使所有方塊堆的高度一樣。如下圖。由於小明是個懶惰的小孩,他想要搬動最小數目的方塊,以達成這個目的,你能幫助他嗎? (輸入規範:每組資料有兩列 第一列有一個數字N,代表有幾堆方塊,第二列有n個數字,分別代表這N堆方塊的高度H。另1<=n<=50、1<=H<=100。方塊的總數一定可以整除堆數N,也就是說,一定可以使所有方塊同高度。)
     ▉ 
     ▉
▉   ▉▉ →
▉ ▉ ▉▉   ▉▉▉▉▉▉
▉ ▉ ▉▉   ▉▉▉▉▉▉
▉▉▉ ▉▉   ▉▉▉▉▉▉
▉▉▉▉▉▉   ▉▉▉▉▉▉

輸入範例:
6
5 2 4 1 7 5
輸出範例:
5

7 則留言:

  1. Dim mycount, num(99), sum, oknum, ans As Integer
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As 2
    Input #1, mycount
    For i = 1 To mycount
    Input #1, num(i)
    sum = sum + num(i)
    Next
    oknum = sum / mycount
    For ii = 1 To mycount
    If num(ii) < oknum Then ans = ans + (oknum - num(ii))
    Next
    Print #2, ans
    Close #2
    Close #1
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Dim ans, y() As Integer
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    ReDim y(n)
    For i = 1 To n
    Input #1, x
    s = s + x
    y(i) = x
    Next
    s = s \ 6
    For i = 1 To n
    If s > y(i) Then ans = ans + (s - y(i))
    Next
    Print #2, ans
    Close
    Close
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim n() As Integer
    Input #1, a
    ReDim n(a)
    Do
    Input #1, n(i)
    'MsgBox n(i)
    i = i + 1
    Loop Until EOF(1)
    For j = 0 To UBound(n)
    q = n(j) + q
    Next
    q = q / a
    'MsgBox q
    For k = 0 To UBound(n)
    If n(k) > q Then w = n(k) - q + w
    Next
    MsgBox w
    Print #2, w
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  4. Dim a()
    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, x
    ReDim a(x)
    For i = 1 To x
    Input #1, a(i)
    t = t + a(i)
    Next
    ap = t \ x
    For i = 1 To x
    If a(i) > ap Then z = z + (a(i) - ap)
    Next
    Print #2, z
    Close
    Close
    End
    End Sub

    回覆刪除
  5. 作者已經移除這則留言。

    回覆刪除
  6. Dim a()
    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, x
    ReDim a(x)
    For i = 1 To x
    Input #1, a(i)
    t = t + a(i)
    Next
    ap = t \ x
    For i = 1 To x
    If a(i) > ap Then z = z + (a(i) - ap)
    Print #2, z
    Next
    Close
    Close
    End
    End Sub

    回覆刪除