Box of Bricks

原创 2013年12月29日 20:15:49

水题一道。。无需多说。。一切尽在代码中。。

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 containsn 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.

#include <stdio.h>
int main()
{
    int n, i, s, aver, a[51], num, j = 0;
    while(scanf("%d", &n)!=EOF)
    {
        if(n == 0) break;
        j++;
        s = 0;
        num = 0;
        for(i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
            s = s + a[i];
        }
        aver = s / n;
        for(i = 0; i < n; i++)
        {
            if(a[i] > aver)
                {
                    num = num + a[i] - aver;
                }
        }
        printf("Set #%d\nThe minimum number of moves is %d.\n\n", j, num);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013013910/article/details/17657829

HDU Box of Bricks

题目传送门: http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1&sectionid=2&problemid=8 本题规律在...
  • u011481047
  • u011481047
  • 2016-03-15 19:00:47
  • 216

杭电 acm Box of Bricks

#include #include using namespace std; int main() { int n,a[100],sum=0,ave,kase=0,coun=0; wh...
  • lgybetter
  • lgybetter
  • 2015-03-31 00:22:03
  • 183

uva 591 Box of Bricks(求平均值)

Box of Bricks  Little Bob likes playing with his box of bricks. He puts the bricks one upon a...
  • u011328934
  • u011328934
  • 2013-07-31 10:25:23
  • 1696

HDOJ(HDU) 2088 Box of Bricks(平均值)

Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one upon an...
  • qq_26525215
  • qq_26525215
  • 2016-04-29 21:41:41
  • 514

HDU1326_Box of Bricks【水题】

题目大意:给你N堆砖块的高度,为了使N堆砖块的高度已知,需要把高的堆上的 砖块移到低的堆上,问最少需要移多少块砖。 思路:计算出总共的砖块数,在求出N堆砖的平均高度(即需要达到的最终高度),把 高的堆...
  • u011676797
  • u011676797
  • 2014-11-21 19:51:45
  • 1143

HDU1326:Box of Bricks

Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one upon an...
  • libin56842
  • libin56842
  • 2013-04-16 15:14:03
  • 3361

UVA - 591 Box of Bricks (水题)

给你n堆高度不一的砖块,问你把这n堆砖块变成相同高度,需要最少移动的砖块数。 开始题目没仔细看,以为照样是求变成相同高度的最小移动次数,这题只要求出平均值,然后累加有多少大于这个均值的即可,因为大于...
  • u012773338
  • u012773338
  • 2014-12-24 20:58:06
  • 249

1477 Box of Bricks

水题,如果说做这个题有什么目的的话,可能就是告诉你要仔细读题,看清楚题目的要求,否则会白白的WA几次啊: #include "stdio.h" int main() { //freopen("in...
  • hackerain
  • hackerain
  • 2011-03-24 19:07:00
  • 828

HDOJ 1326 Box of Bricks(简单题)

Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one upon an...
  • qq_26525215
  • qq_26525215
  • 2016-04-08 01:14:41
  • 697
收藏助手
不良信息举报
您举报文章:Box of Bricks
举报原因:
原因补充:

(最多只允许输入30个字)