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?
![](http://icpc.ahu.edu.cn/OJ/images/1011.jpg)
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.
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.
Output a blank line after each set.
Sample Input
Original | Transformed |
6 5 2 4 1 7 5 0
Sample Output
Original | Transformed |
Set #1 The minimum number of moves is 5.
欸。。题目很简单,干嘛要整个英文呢~害的我好半天都是“presentation error” 原来题目有要求要每个输出之后有一个空行。。
没有什么新鲜的地方,唯一值得提的就是vector容器的clean()操作,就是把这个容器全部某掉,在这个多组数据输出题目中真的很必要,当然这个操作也体现了容器的优势,要是用数组存储数据的话又要用一个循环才能清除了~想到英国人的一句谚语‘不要再重复发明轮子’
C++ 的STL真的很方便,这句话用来形容STL 真的再合适不过了~。代码直接贴上:
//AOJ 12 box of bricks
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n,temp,sum,avg,steps,num;
cin>>n;
num=0;
vector<int> vec;
while(n!=0){
num++;
sum=0;
steps=0;
for(int i=1;i<=n;i++){
cin>>temp;
vec.push_back(temp);
sum+=temp;
}
avg=sum/n;
for(vector<int>::iterator iter=vec.begin();iter!=vec.end();iter++)
if(*iter<avg) steps+=avg-*iter;
cout<<"Set #"<<num<<endl;
cout<<"The minimum number of moves is "<<steps<<"."<<endl;
cout<<endl;
cin>>n;
vec.clear();
}
return 0;
}