341人阅读 评论(0)

Box of Bricks

Problem 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. 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 <= 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 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.



Source

Southwestern Europe 1997

#include <stdio.h>
#define M 100

int n,i,ss,yi,arr,kk=0;

int main()
{
int num[M];
while(scanf("%d",&n)&&(n!=0))
{
ss=0; yi=0;
kk++;
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
ss+=num[i];
}
arr=ss/n;
for(i=0;i<n;i++)
if(num[i]>arr) yi+=(num[i]-arr);
printf("Set #%d\n",kk);
printf("The minimum number of moves is %d.\n",yi);
printf("\n");
}
return 0;
}

0
0

【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐（算法+实战）--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：12140次
• 积分：6
• 等级：
• 排名：千里之外
• 原创：25篇
• 转载：0篇
• 译文：0篇
• 评论：0条
文章分类
阅读排行