Ultraviolet A 10137 the trip

Source: Internet
Author: User
Tags cmath

Every small exercise has some gains. Today I have strengthened the precision control,

The trip

A number of students are members of a club that travels annually to exotic locations. Their destinations in the past have included indianapois, 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 particle 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
 
310.0000000030.00415.0015.013.003.010
Output for sample input
 
$10.00 $11.99

The idea is very simple. Calculate the average value of the total amount of money and then calculate the amount of money to be transferred. However, pay attention that the minimum unit of the amount of money is 0.01. Therefore, it is very important to process the average value,

Sum = sum/per + 0.005; this is the rounding method. Add 0.005. If the third digit after the decimal point has an increment, it indicates the third digit> 5,

Sum = floor (sum * 100)/100.0; floor (sum * 100) is to discard the third and subsequent digits of the original sum decimal point,

Another thing to note is that there should be two variables for the amount of money transferred. One record is larger than the average value, and the other record is smaller than the average value, because the average value has been processed before, therefore, such variables may not be equal.

Code:

 # include 
  
    # include 
   
     # include 
    
      using namespace STD; int main () {int per = 10, I; double num [1010], sum, a, B; while (scanf ("% d", & per )! = EOF & per) {sum = 0; for (I = 0; I 
     
       1e-12) A + = num [I]-sum; else B + = sum-num [I];} If (A> B) printf ("$ %. 2lf \ n ", B); else printf (" $ %. 2lf \ n ", a) ;}return 0 ;}
     
    
   
  

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.