The Trip Programming challenge UVA10137 I have the following solution for the Trip problem from Skiena's and Revilla's Programming Challenges (full problem description here).

//#include "stdafx.h"
#include &lt;iostream&gt;
#include &lt;string&gt; //Always import &lt;string&gt; if piping std input to a string in .net 
#include&lt;algorithm&gt;
#include&lt;math.h&gt;
#include&lt;fstream&gt;
using namespace std; 
int main()
{
    //ofstream myfile;
    //myfile.open("example.txt");

    int n; //number of students sharing trip costs.
    while (cin &gt;&gt; n) { 
        if (n == 0) {
            return 0;
        }

        double *ammounts_spent = new double[n];
        double total_spent = 0;
        for (int i = 0; i &lt; n; i++) {
            cin &gt;&gt; ammounts_spent[i];
            total_spent += ammounts_spent[i];
        }
        double ammount_exchanged = 0; 
        double ammount_exchanged_below = 0;
        double ammount_exchanged_above = 0;

        double average_spent = total_spent / n;
        double rounded_down_average_spent =(double)((int)(average_spent * 100)/(double)100);
        double rounded_up_average_spent = rounded_down_average_spent + 0.01;
        if (rounded_down_average_spent - average_spent &lt; 0) {
            for (int i = 0; i &lt; n; i++) {
                if (average_spent &gt; ammounts_spent[i]) {
                    ammount_exchanged_below += rounded_down_average_spent - ammounts_spent[i];
                }
                else {
                    ammount_exchanged_above += ammounts_spent[i] - rounded_up_average_spent;
                }
            }


        }
        else {
            for (int i = 0; i &lt; n; i++) {
                if (average_spent &gt; ammounts_spent[i]) {
                    ammount_exchanged_below += average_spent - ammounts_spent[i];
                }
                else {
                    ammount_exchanged_above += ammounts_spent[i] -average_spent;
                }
            }

        }

        ammount_exchanged = max(ammount_exchanged_above, ammount_exchanged_below);
        if (ammount_exchanged == floor(ammount_exchanged)) {
            cout &lt;&lt; "$" &lt;&lt; ammount_exchanged &lt;&lt; ".00\n";

        }
        else {
            cout &lt;&lt; "$" &lt;&lt; ammount_exchanged &lt;&lt; "\n";
        }
    }
}


It works for all debug input given on udebug and even for tricky instances such as 

&gt; 4
&gt; 1.01
&gt; 1.01
&gt; 1.00
&gt; 1.00


Where the amount exchanged is $0.00. Yet the UVA online judge is giving me a verdict of wrong answer.
Printing loses precision

Using this input file:

6
0 0 0 0 10000 10000


Your program gives this answer:


$13333.3



The actual answer should be $13333.32.  The program seemed correct, but the printing only printed out 6 digits of precision.  I made this change to your program which made it print out the correct answer:

cout.precision(15);


Better to use integers

It would be safer to use integers throughout your program.  There could be any number of floating point roundoff problems that might show up unexpectedly, especially since 0.01 is not perfectly representable by a double.