UVa 10137 The trip: Decimal rounding & places to be noted

Source: Internet
Author: User
Tags time limit

10137-the Trip

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=29&page=show_problem &problem=1078

A number of students are members of the 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 trips to Eindhoven.

The group agrees in advance to share expenses equally, but it isn't, practical to have them, share, every as it expense Rs. So individuals in the group pay for particular things, like meals, hotels, taxi rides, plane tickets, etc. After the trips, each student ' s expenses are tallied and money are exchanged so ' the ' of ' the ' the ' to ' the ' same Thin one cent. In the past, this is the exchange has been tedious and time consuming. Your job is to compute, from a list of expenses, the minimum amount of money this must change hands in order to equalize ( Within a cent) all the students ' costs.

The Input

Standard input would contain the information for several trips. The information for each trips consists of a line containing a positive integers, n, the number of students on the trip, fol Lowed 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. A single line containing 0 follows the "information for the" last trip.

The Output

For all trips, output a line stating the total amount of, in dollars and cents, that must is exchanged to equalize T He students ' costs.

Sample Input

3
10.00
20.00
30.00
4
15.00
15.01
3.00
3.01
0

Output for Sample Input

$10.00
$11.99

Complete code:

/*0.019s*/
      
#include <cstdio>  
#include <cmath>  
#include <algorithm>  
using namespace Std;  
      
Double x[1005];  
      
int main ()  
{  
    int n;  
    Double sum, sum2, ave;  
    while (scanf ("%d", &n), N)  
    {  
        sum = 0.0;  
        for (int i = 0; i < n; ++i)  
        {  
            scanf ("%lf", &x[i));  
            Sum + + x[i];  
        }  
        Ave = Round (sum/n * 100)/100;///retains the decimal point after 2 digits  
        sum = sum2 = 0.0;  
        for (int i = 0; i < n; ++i)  
            if (X[i] < ave) sum = ave-x[i];  
            else sum2 + = X[i]-ave;///Although it is possible to analyze the rounding above, it is slightly higher in programming complexity than  
        printf ("$%.2f\n", min (sum, sum2));  
    }  
    return 0;  
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.