591 Box of Bricks

真心好水的题。。。。 只需要求出砖块数目的平均值 ,再用每一摞砖的数目减去平均值 ,只取相减后大于0的或者只取小于零的相加 得到的和就是最少次数

注意输出两个回车 题目及AC代码如下

  Box of Bricks 

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?

 

 

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 Ÿ\le n \leŸ 50$ and $1 \leŸ h_i Ÿ\le 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.

 

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.

 

Sample Input 

6
5 2 4 1 7 5
0

 

Sample Output 

Set #1
The minimum number of moves is 5.


 1 // 591.cpp : 定义控制台应用程序的入口点。
 2 //
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 #include<string.h>
 6 
 7 
 8 int main(void)
 9 {
10     int n;
11     int arr[55];
12     int sum;
13     int count = 1;
14     while (scanf("%d", &n) && n)
15     {
16         memset(arr, 0, sizeof(arr));
17         sum = 0;
18         for (int i = 0; i < n; i++)
19         {
20             scanf("%d", &arr[i]);
21             sum += arr[i];
22         }
23         sum = sum / n;
24         int times = 0;
25         for (int i = 0; i < n; i++)
26         {
27             if (arr[i] - sum>0) times += (arr[i] - sum);
28         }
29         printf("Set #%d\n", count++);
30         printf("The minimum number of moves is %d.\n\n", times);
31     }
32     return 0;
33 }
34 
35                 

 

posted @ 2014-03-08 15:45  VOID修罗  阅读(260)  评论(0编辑  收藏  举报