HDU 2088 Box of Bricks

来源:互联网 发布:辐射4 捏脸数据 编辑:程序博客网 时间:2024/05/21 01:29

http://acm.hdu.edu.cn/showproblem.php?pid=2088

 

Box of Bricks

Time Limit: 1000/1000 MS(Java/Others)    MemoryLimit: 32768/32768 K (Java/Others)
Total Submission(s):5277    AcceptedSubmission(s): 1838


Problem Description
Little Bob likes playing with his box ofbricks. He puts the bricks one upon another and builds stacks ofdifferent height. “Look, I've built a wall!”, he tells his oldersister Alice. “Nah, you should make all stacks the same height.Then you would have a real wall.”, she retorts. After a littleconsideration, Bob sees that she is right. So he sets out torearrange the bricks, one by one, such that all stacks are the sameheight afterwards. But since Bob is lazy he wants to do this withthe minimum number of bricks moved. Can you help?
HDU <wbr>2088 <wbr>Box <wbr>of <wbr>Bricks
 


 

Input
The input consists of several data sets.Each set begins with a line containing the number n of stacks Bobhas built. The next line contains n numbers, the heights hi of then stacks. You may assume 1≤n≤50 and 1≤hi≤100.

The total number of bricks will be divisible by the number ofstacks. Thus, it is always possible to rearrange the bricks suchthat all stacks have the same height.

The input is terminated by a set starting with n = 0. This setshould not be processed.
 


 

Output
For each set, print the minimum numberof bricks that have to be moved in order to make all the stacks thesame height.
Output a blank line between each set.
 


 

Sample Input
6 5 2 4 1 75 0
 


 

Sample Output
5
 


 

Author
qianneng
 


 

Source
冬练三九之二
 


 

Recommend
lcy
 
分析:简单题,轻轻地AC之。。
代码如下:
#include<stdio.h>
#include<string.h>
#include<math.h>
int abs(int x)
{
 return x>0?x:-x;
}
int main()
{
    intn,i,hei,ans;
    intw[51];
    intcount=0;
   while(scanf("%d",&n),n)
    {
    if(count!=0) printf("\n");
    ans=hei=0;
    for(i=0;i<n;i++)
      {
      scanf("%d",&w[i]);
      hei+=w[i];
      }
     hei=hei/n;
       for(i=0;i<n;i++)
        ans+=abs(hei-w[i]);
     printf("%d\n",ans/2);
     count++;
    }
 return 0;
}
原创粉丝点击