Yo, ggll, good sample data. I posted on other threads, including one of my onw, about my frustrations with this problem, but after using that sample data I was able to change one line and life was good again.
Thanks.
Moderator: Board moderators
ans += trip[i] - avg;/* @JUDGE_ID: 36781AT 10137 C++ */
#include <iostream>
using namespace std;
int min(int num1, int num2) {
if (num1 < num2) {
return num1;
} else {
return num2;
}
}
int max(int num1, int num2) {
if (num1 > num2) {
return num1;
} else {
return num2;
}
}
int calcBest(int cents[], int avg, int numPpl) {
int top = 0, bottom = 0, middle = 0;
for (int i = 0; i < numPpl; i++) {
if (cents[i] > avg) {
top += cents[i] - avg;
} else if (cents[i] < avg) {
bottom += avg - cents[i];
}
}
if (top == 0) {
return calcBest(cents, --avg, numPpl);
} else if (bottom == 0) {
return calcBest(cents, ++avg, numPpl);
}
return min(top, bottom);
}
int main() {
int cents[1000];
int numPpl, avg, best, top, bottom;
double temp;
while ((cin >> numPpl) && (numPpl != 0)) {
top = avg = 0;
bottom = 10000000;
for (int i = 0; i < numPpl; i++) {
cin >> temp;
avg += (int) (temp * 10000 + 1e-9) / numPpl;
cents[i] = (int) (temp * 100 + 1e-9);
top = max(top, cents[i]);
bottom = min(bottom, cents[i]);
}
if (abs(top - bottom) <= 1) {
best = 0;
} else {
avg = (avg + 50) / 100;
best = calcBest(cents, avg, numPpl);
}
cout << "$" << (best / 100) << "." << (best % 100) / 10 << (best % 10) << endl;
}
return 0;
}Users browsing this forum: No registered users and 0 guests