http://acm.hit.edu.cn/hoj/problem/view?id=1370
不能平分就输出-1
N很大 数组开大一点
#include <stdio.h>
#include <math.h>
int block[16384];
double average(int block[], int size);
bool isint(double x);
int main()
{
int i, n, sum;
double avg;
while (scanf("%d", &n) != EOF)
{
if (n == -1)
break;
sum = 0;
for (i = 1; i <= n; i++)
scanf("%d", &block[i]);
avg = average(block, n);
if (isint(avg))
{
for (i = 1; i <= n; i++)
{
if (block[i] > avg)
sum += block[i]-avg;
}
printf("%d\n", sum);
}
else
printf("-1\n");
}
}
double average(int block[], int size)
{
double s = 0;
int i;
for (i = 1; i <= size; i++)
s += block[i];
return s/size;
}
bool isint(double x)
{
const double eps = 10e-12;
double z;
z = fmod(x, (int)x);
if (z < eps)
return 1;
return 0;
}