Question 70: Box of Bricks (ACM)

Source: Internet
Author: User

[Plain]

[Plain] 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. "Hannah, you shoshould make all stacks the same height. then you wowould 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 <= 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 shoshould 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.

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. "Hannah, you shoshould make all stacks the same height. then you wowould 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 <= 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 shoshould 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.

 


[Plain]
# Include <stdio. h>
 
Int main ()
{
Int n;
Int I;
Int sum;
Int average;
Int num;
Int j = 1;
Int a [100];
 
While (scanf ("% d", & n), n! = 0)
{
Sum = num = 0;
For (I = 0; I <n; I ++)
{
Scanf ("% d", & a [I]);
}
 
For (I = 0; I <n; I ++)
{
Sum + = a [I]; // total number
}
Average = sum/n;

For (I = 0; I <n; I ++) // calculates the number of boxes to move
{
If (a [I]> average)
{
Num = num + a [I]-average;
}
}
 
Printf ("Set # % d \ n", j );
Printf ("The minimum number of moves is % d. \ n", num );
 
J ++;
}
 
Return 0;
}

# Include <stdio. h>

Int main ()
{
Int n;
Int I;
Int sum;
Int average;
Int num;
Int j = 1;
Int a [100];

While (scanf ("% d", & n), n! = 0)
{
Sum = num = 0;
For (I = 0; I <n; I ++)
{
Scanf ("% d", & a [I]);
}

For (I = 0; I <n; I ++)
{
Sum + = a [I]; // total number
}
Average = sum/n;

For (I = 0; I <n; I ++) // calculates the number of boxes to move
{
If (a [I]> average)
{
Num = num + a [I]-average;
}
}

Printf ("Set # % d \ n", j );
Printf ("The minimum number of moves is % d. \ n", num );

J ++;
}

Return 0;
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.