Box of Bricks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4339 Accepted Submission(s): 1946
Problem Description
Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. ``Look, I've built a wall!'', he tells his older sister Alice. ``Nah, you should make all stacks the same height. Then you would have a real wall.'', she retorts. After a little con- sideration, Bob sees that she is right. So he sets out to rearrange the bricks, one by one, such that all stacks are the same height afterwards. But since Bob is lazy he wants to do this with the minimum number of bricks moved. Can you help?
![](https://i-blog.csdnimg.cn/blog_migrate/b876fedc091574a452d8cca9c5753d8f.gif)
Input
The input consists of several data sets. Each set begins with a line containing the number n of stacks Bob has built. The next line contains n numbers, the heights hi of the n stacks. You may assume 1 <= n <= 50 and 1 <= hi <= 100.
The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.
The input is terminated by a set starting with n = 0. This set should not be processed.
The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.
The input is terminated by a set starting with n = 0. This set should not be processed.
Output
For each set, first print the number of the set, as shown in the sample output. Then print the line ``The minimum number of moves is k.'', where k is the minimum number of bricks that have to be moved in order to make all the stacks the same height.
Output a blank line after each set.
Output a blank line after each set.
Sample Input
6 5 2 4 1 7 5 0
Sample Output
Set #1 The minimum number of moves is 5.题目翻译:小鲍勃喜欢打他的砖框。他把别人身上的砖块,并建立不同高度堆栈。 “你看,我已经建立了一堵墙!”,他告诉他的姐姐爱丽丝。 “不,你应该让所有堆栈相同的高度,然后你将有一个真正的的墙。”,她反驳。一点点的审议后,鲍勃认为,她是正确的。因此,他下决心重新排列的砖块,一个接一个,如所有栈是相同的高度之后的。但是,由于Bob是懒惰的,他想要做移动砖的最低数量。你能帮助吗? 输入 输入由几个数据集。每套包含栈鲍勃已建成的数n开始。下一行包含n个数字,高度的N堆栈您好。你可以假设1<= N <= 50和1<=您好<= 100。 砖的总数将堆栈数整除。因此,它总是有可能重新排列砖块等所有协议栈具有相同的高度。 输入是一套n = 0时开始终止。此设置不应该处理。 输出 对于每一个设置,第一次印刷的设置数量,如示例输出所示。然后打印行“的举动的最低数量为k”,其中k是砖,以使所有的堆叠相同的高度,必须移动的最小数目。 输出每套后的空白行。 样例输入 6 5 2 41 7 5 0 输出范例 第1集 动作的最低数量为5。解题思路:题目要求的是 最小搬动次数, 那么只要求出平均值, 大于平均值的墙就是要移动的墙 ,累加就可以了. 附代码:#include <stdio.h> #include <string.h> int main() { int i,k,n,m,sum,num; int a[55];k=1; while(scanf("%d",&n),n){ sum=0;memset(a,0,sizeof(a)); for(i=0;i<n;i++){ scanf("%d",&a[i]); sum+=a[i]; } sum/=n;num=0; for(i=0;i<n;i++) if(a[i]>sum) num+=a[i]-sum; printf("Set #%d\nThe minimum number of moves is %d.\n\n",k++,num); } return 0; }