-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrip.cpp
47 lines (37 loc) · 1.14 KB
/
trip.cpp
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
/*
* File: trip.cpp
* Author: shijiex
*
* Created on November 7, 2013, 2:17 PM
*/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>
using namespace std;
int main(int argc, const char * argv[]) {
int numStudents = 0;
while (scanf("%d\n", &numStudents) != EOF) {
if (numStudents == 0) break;
vector<double> students;
double total = 0;
for (int i = 0; i < numStudents; i++) {
double money = 0;
scanf("%lf\n", &money);
students.push_back(money);
total += students.at(i);
}
double average = total / numStudents;
double pos = 0, neg = 0;
for (vector<double>::iterator iter = students.begin(); iter != students.end(); iter++) {
// Force cast to int so that all digits after 0.01 would be removed
double dis = (int) (((double) (*iter) - average)*100) / 100.0;
if (dis < 0) neg += dis;
else pos += dis;
}
double exchange = (-neg > pos) ? -neg : pos;
cout.precision(2);
cout <<std::fixed<<"$"<<exchange << endl;
}
return 0;
}