-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva10137.c
51 lines (48 loc) · 1.48 KB
/
uva10137.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* UVA 10137 - The Trip
* https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=13&page=show_problem&problem=1078
* $ g++ uva10137.c -lm -lcrypt -O2 -pipe -DONLINE_JUDGE
*/
#include <math.h>
#include <stdio.h>
int main(void) {
const int MAX_STUDENTS = 1000;
int n = 0;
int amounts[MAX_STUDENTS];
while(scanf("%d", &n) != EOF) {
if (n == 0) {
break;
}
int dollars = 0;
int cents = 0;
char sep;
int ceil_avg, average_cents = 0;
int under = 0;
int over = 0;
int exchange = 0;
for (int i = 0; i < n; i++) {
scanf("%d%c%d", &dollars, &sep, ¢s);
amounts[i] = dollars * 100 + cents;
average_cents += amounts[i];
}
// This is the not-obvious special case!
// Depending on n and the amount to be divided, we need to check
// if rounding up or down to the nearest cent is more efficient.
ceil_avg = ceil(1.0 * average_cents / n);
average_cents /= n;
for (int i = 0; i < n; i++) {
if(amounts[i] > ceil_avg) {
over += amounts[i] - ceil_avg;
}
else if (amounts[i] < average_cents) {
under += average_cents - amounts[i];
}
}
exchange = over;
if (under > over) {
exchange = under;
}
printf("$%d.%02d\n", exchange / 100, exchange % 100);
}
return 0;
}