题意:有n个数,问当把所有的数值均匀分配,需要移动多少个1.
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=55;
int iabs(int a){return a>0?a:-a;}
int main()
{
int n,a[N],t_cnt=0;
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
int sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
int ave=sum/n,imin=0;
for(int i=0;i<n;i++)
{
imin+=iabs(a[i]-ave);
}
printf("Set #%d\n",++t_cnt);
printf("The minimum number of moves is %d.\n\n",imin/2);
}
return 0;
}