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 0Sample Output
Set #1 The minimum number of moves is 5. 题意: 水题一道,就是算出将数列柱子变成一样高,要动多少次。 分析: 算出平均高度,在循环找出低于平均高度的列,累加高出的部分总和 #include<iostream> #include<vector> #include<string> #include<set> #include<map> #include<algorithm> #include<string.h> using namespace std; int main() { int m,n,sum=0,ave=0,h=0,b=1; vector<int>a; while(1) { cin>>n; if(n==0){break;} for(int i=0;i<n;i++) { cin>>m; sum=sum+m; a.push_back(m); } ave=sum/n; for(int i=0;i<n;i++) { if(a[i]>ave) { h=h+(a[i]-ave); } } cout<<"Set #"<<b<<endl; cout<<"The minimum number of moves is "<<h<<"."<<endl<<endl; a.clear();b=b+1; sum=0;h=0;ave=0; } }