E - Box of Bricks (注意很多文章是错的)
InputThe 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.
OutputFor each set, print the minimum number of bricks that have to be moved in order to make all the stacks the same height.
Output a blank line between each set.
Sample Input
6 5 2 4 1 7 5 0
Sample Output
5
这道题非常简单,但是输出格式有点儿坑,尤其对于我这种才坑的人来说。在这里批评很多博客主只是为了经验!根本不管这道题的考察的是什么。
这道题注重的是输出格式。要保证输入0前没有空行,而在两组数据之间必须有空行。(想想都是气)
代码如下:(大家注意看如何保障输入0时没空行的操作)
#include<iostream>
using namespace std;
int num[51];
int main()
{
int n;
int t = 0;
while (cin >> n&&n)
{
if (t != 0)cout << endl;
t = 1;
for (int i = 0; i < n; i++)
{
cin >> num[i];
}
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += num[i];
}
sum = sum / n;
int Sum = 0;
for (int i = 0; i < n; i++)
{
if (num[i]>sum)
{
Sum += num[i] - sum;
}
}
printf("%d\n", Sum);
}
}