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 ;}