Skip to content

Instantly share code, notes, and snippets.

@Abreto
Created August 9, 2014 13:39
Show Gist options
  • Select an option

  • Save Abreto/9c47bd74f63eb24b6c5c to your computer and use it in GitHub Desktop.

Select an option

Save Abreto/9c47bd74f63eb24b6c5c to your computer and use it in GitHub Desktop.
UVa 10137 - 旅行{The Trip}
/* Programming-challenges 110103 - The Trip . Abreto. */
#include <stdio.h>
int main(void)
{
int i = 0;
int n = 0;
while( scanf("%d", &n) != EOF )
{
double costs[1024] = {0.0};
double sum = 0.0, avg = 0.0;
double minex = 0.0, minex2 = 0.0;
if( 0 == n )
break;
for(i = 0;i < n;++i)
{
scanf("%lf", &(costs[i]));
sum += costs[i];
}
avg = sum/n;
avg = ((int)(avg*100+0.5))/100.0;
for(i = 0;i < n;++i)
if( costs[i] > avg )
{
double diff = 0.0;
diff = costs[i] - avg;
diff = ((int)(diff*100+0.5))/100.0;
minex += diff;
minex = ((int)(minex*100+0.5))/100.0;
}
else if ( costs[i] < avg )
{
double diff = 0.0;
diff = avg - costs[i];
diff = ((int)(diff*100+0.5))/100.0;
minex2 += diff;
minex2 = ((int)(minex2*100+0.5))/100.0;
}
printf("$%.2lf\n", minex<minex2?minex:minex2);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment