题意:n个高矮不等的堆。每次可以从一个堆拿一块移动到另一堆。问起码移动几许次能使他们全都相等。
思路:好经典的题目,看到过不只1次了。所有数的平均数与所有的数的差的绝对值加起来除以二。
即 sum/n; ans += abs(sum-a[i]); ans/=2;
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int SIZE = 101;
int a[SIZE];
int main()
{
int n;
int times = 0;
while(scanf("%d", &n), n)
{
int sum = 0;
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
sum += a[i];
}
sum /= n;
int ans = 0;
for(int i = 0; i < n; i++)
{
ans += abs(sum-a[i]);
}
printf("Set #%d\n", ++times);
printf("The minimum number of moves is %d.\n\n", ans/2);
}
return 0;
}
#include <stdlib.h>
#include <string.h>
using namespace std;
const int SIZE = 101;
int a[SIZE];
int main()
{
int n;
int times = 0;
while(scanf("%d", &n), n)
{
int sum = 0;
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
sum += a[i];
}
sum /= n;
int ans = 0;
for(int i = 0; i < n; i++)
{
ans += abs(sum-a[i]);
}
printf("Set #%d\n", ++times);
printf("The minimum number of moves is %d.\n\n", ans/2);
}
return 0;
}