591-box of Bricks
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php? Option=com_onlinejudge&itemid=8&category=467&page=show_problem&problem=53 2
Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different. ' Look, I ' ve built a wall! ' and 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, she's right. So it sets out to rearrange of the bricks, one by one, such which all stacks are the same height afterwards. But since Bob is lazy him wants to does this with the minimum number of bricks. Can you help?
and.
The total number of bricks is divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such this all stacks the have height.
The input is terminated by a set starting with n = 0. This set should is processed.
Output
For each set, the "number of the" set, the as shown in the sample output. Then Print the "minimum number ofmoves is K.", where k is the minimum number of bricks so have to B E 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.
Water.
Complete code:
/*0.016s*/
#include <cstdio>
int a[50];
int main (void)
{
int n, i, case = 1, sum, AVR, ans;
while (scanf ("%d", &n), N)
{
sum = 0;
Ans = 0;
for (i = 0; i < n; i++)
{
scanf ("%d", &a[i]);
Sum + + a[i];
}
AVR = sum/n;
for (i = 0; i < n; i++)
if (A[i] > AVR)
ans + = (A[i]-AVR);
printf ("Set #%d\nthe minimum number of moves is%d.\n\n", case++, ans);
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/