Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Here is a problem from the book Programming Challenges by Skiena and Revilla:

The Trip

A number of students are members of a club that travels annually to exotic locations. Their destinations in the past have included Indianapolis, Phoenix, Nashville, Philadelphia, San Jose, and Atlanta. This spring they are planning a trip to Eindhoven. The group agrees in advance to share expenses equally, but it is not practical to have them share every expense as it occurs. So individuals in the group pay for particular things, like meals, hotels, taxi rides, plane tickets, etc. After the trip, each student's expenses are tallied and money is exchanged so that the net cost to each is the same, to within one cent. In the past, this money exchange has been tedious and time consuming. Your job is to compute, from a list of expenses, the minimum amount of money that must change hands in order to equalize (within a cent) all the students' costs.

The Input

Standard input will contain the information for several trips. The information for each trip consists of a line containing a positive integer, n, the number of students on the trip, followed by n lines of input, each containing the amount, in dollars and cents, spent by a student. There are no more than 1000 students and no student spent more than $10,000.00. A single line containing 0 follows the information for the last trip.

The Output

For each trip, output a line stating the total amount of money, in dollars and cents, that must be exchanged to equalize the students' costs.

Sample Input

3
10.00
20.00
30.00
4
15.00
15.01
3.00
3.01
0

Sample Output

$10.00 
$11.99

Here is my solution which gives correct answer for all inputs I have tried so far, but there must be some case when it does not work because the judge says so. Can anybody help me to understand why?

int main (int argc, char * argv[]) {
    int64_t a;
    float paid[1000];
    std::vector<float> results;
    while (std::cin >> a && a != 0) {

        int sum = 0;
        for (int i = 0; i < a; i++) {
            std::cin >> paid[i];
            paid[i] *= 100;
            sum += paid[i];
        }
        int result = 0;
        int avg = (std::ceil)((float)sum/a);

        int mod = sum % a;
        int c = 0;
        if (mod > 0) {
            for (int i = 0; i < a; i++) {
                if (c >= a - mod)
                    break;
                if (paid[i] < avg) {
                    paid[i]++;
                    c++;
                }
            }
            for (int i = 0; i < a; i++) {
                if (c >= a - mod)
                    break;
                if (paid[i] > avg) {
                    paid[i] += a - mod - c;
                    break;
                }
            }
        }

        for (int i = 0; i < a; i++) {
            if (paid[i] > avg) {
                result += (paid[i] - avg);
            }
        }

        results.push_back((float)result/100);
    }
    std::cout << std::fixed;
    std::cout << std::setprecision(2);
    for (std::vector<float>::iterator it = results.begin(); it != results.end(); ++it) {
        std::cout << "$" << *it << std::endl;
    }
    return 0;
}
share|improve this question
    
My C++ is very rusty, but I can suggest some possible test cases which might break it. What happens if one student doesn't have any expenses? More than one? What if one student pays for everything? What if the total bill is smaller than the number of students? Do you return 0 if everyone paid equal amounts? What about if everyone was within one cent of each other? –  Bobson Nov 15 '12 at 17:27
    
@Bobson thanks for suggestion for the test cases. I tried but do not see any wrong solution: 10.01 0 -> $5.00 (2 students); 10.01 0 0 -> $6.67; 3.01 1.11 0 0 0 -> $2.46; 3.01 3.00 3.00 -> $0.00 –  Jana Nov 15 '12 at 20:50
    
What about 0 0 0 -> 0? You might get a divide-by-zero error somewhere in that case. It's the only thing I can think of. Good luck! –  Bobson Nov 15 '12 at 21:59
    
0 0 is not possible to input, the program exits when you input 0 students –  Jana Nov 16 '12 at 1:58
    
Sorry, I meant 0.00 0.00 0.00 -> $0.00. –  Bobson Nov 16 '12 at 14:28

1 Answer 1

Your algorithm seems to be correct though a bit overcomplicated. But the code is not correct. consider:

9999.1
9999.1
9999.0
9999.1

$0.05

And the correct answer is $0.07

This is due to lack of precision in float implementation. It does not mean you should use double, it mean you should check your casts and try to avoid using float at money computations at all.

Also, i suggest to check your inputs.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.