此题不是太复杂,通过题目中的
But since Bob is lazy he wants to do this with the minimum number of bricks moved.
这一句话可知要求的移动的的砖块最少既是让我们尽可能少的移动砖块。
首先是通过while循环以保证在输入0之前程序可以一直不停的执行。而while循环中的n则是表示这一堵墙共有多少列,在while循环中在通过for循环输入n个数,存入数组a[i],这n个数则表示初始时这一堵墙每一列的砖的数目。然后通过:砖的总数/列数n 求出每一列应有的砖的数目而最小的移动次数则为 所有超过平均数的数–平均数 的总和
或者 平均数–所有超过平均数的数 的总和
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int n, i, hi, sum, b, c;
int a[101];
c = 0;
while (scanf_s("%d", &n) && n != 0)
{
hi = 0;
b = 0;
sum = 0;
for (i = 0; i < n; i++)
{
scanf_s("%d",&a[i]);//不可写成scanf_s("%d ",&a[i]);这种形式,数组中的输入自带空格
sum += a[i];
}
hi = sum / n;
for (i = 0; i < n; i++)
{
if (a[i] > hi)
b = b + a[i] - hi;
}
/*
for (i = 0; i < n; i++)
{
if (a[i] < hi)
b = b + hi - a[i];
}
*/
c++;
printf("Set #%d\n",c);
printf("The minimum number of moves is %d.\n\n",b);
}
return 0;
}