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/