# UVa 591 ### 題目連結:[UVa591](http://domen111.github.io/UVa-Easy-Viewer/?591) ### 題述: 3 歲的小明喜歡玩他的方塊積木,他總是把方塊疊在一起形成高度不一的方塊堆。然後他說:這是一面牆。 5 歲的姊姊小美聽到了就跟小明說:真正的牆高度應該要一樣才行。小明聽了覺得有道理於是決定要搬動一些方塊使所有方塊堆的高度一樣。如下圖。由於小明是個懶惰的小孩,他想要搬動最小數目的方塊以達成這個目的,你能幫助他嗎?![](https://i.imgur.com/Z2goJaF.png) --- 輸入包含好幾組資料,每組資料有2行,第一行有一個數字 n ,代表有幾堆方塊。第二行有 n 個數字分別代表這 n 堆方塊的高度 hi 。你可以假設 1 <= n <= 50 ; 1 <= hi <= 100 方塊的總數一定可以整除堆數 n ,也就是說一定可以使所有的方塊堆同樣高度。 如果輸入的 n = 0 ,代表輸入結束。 對每一組輸入資料,首先輸出一行這是第幾組測試資料,下一行為 "The minimum number of moves is k." k 在這裡就是需搬動方塊最小的數目以使所有的方塊堆同一高度。每組測試資料後亦請空一行。請參考 Sample Output. ### c code: ```c= #include<stdio.h> int main() { int n ; int i ; int b[50] ; int total ; int high ; int ans ; int count = 0 ; scanf ( "%d" , &n ) ; while ( n != 0 ) { total = 0 ; ans = 0 ; count++ ; for ( i = 0 ; i < n ; i++ ) { scanf ( "%d" , &b[i] ) ; total += b[i] ; } high = total / n ; for ( i = 0 ; i < n ; i++ ) { if ( b[i] < high ) { ans += ( high - b[i] ) ; } } printf ( "Set #%d\n" , count ) ; printf ( "The minimum number of moves is %d.\n\n" , ans ) ; scanf ( "%d" , &n ) ; } } ``` ![](https://i.imgur.com/eBc2bdf.png) :::success **``sample input``** 6 5 2 4 1 7 5 3 1 1 1 0 ::: :::success **``sample output``** Set #1 The minimum number of moves is 5. Set #2 The minimum number of moves is 0. ::: #### [返回首頁](https://hackmd.io/@fkleofk/APCS#591) ###### tags: `APCS選修` `C++` `UVa`