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