1301. 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.

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.

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

Source: Waterloo
Local Contest Jan. 31, 1999

几点注意:

1、

2、

3、

4、

 1 /*
 2 #include <stdio.h>
 3 #include <string.h>
 4
 5 float cost[100];
 6 int cost100[100];
 7
 8 void sort(int num);
 9
10 int main(void)
11 {
12     int num,i,sum=0;
13     int avg,last;
14     int change_sum=0;
15     double change;
16
17     scanf("%d",&num);
18     while(num != 0){
19         for(i=0;i<num;i++){
20             scanf("%f",&cost[i]);
21             cost100[i] = 100*cost[i] + 0.5;
22             sum += cost100[i];
23         }
24         avg = sum/num;
25         last = sum%num;
26         sort(num);
27         for(i=0;i<num;i++){
28             if(i<last)
29                 cost100[i] = cost100[i] - avg - 1;
30             else
31                 cost100[i] = cost100[i] - avg;
32             if(cost100[i] > 0)
33                 change_sum += cost100[i];
34         }
35
36         change = ((double)change_sum)/100;
37         printf("$%.2f\n",change);
38
39         memset(cost,0,num*sizeof(float));
40         memset(cost100,0,num*sizeof(int));
41
42         sum = 0;
43         change_sum = 0;
44
45         scanf("%d",&num);
46     }
47
48     //system("PAUSE");
49     return 0;
50 }
51 void sort(int num)
52 {
53      int i,j;
54      for(i=0;i<num;i++){
55          for(j=0;j<num;j++){
56              if(cost100[j]<cost100[j+1]){
57                  int temp = cost100[j];
58                  cost100[j] = cost100[j+1];
59                  cost100[j+1] = temp;
60              }
61          }
62      }
63
64 }
65  */
66
67
68 #include <stdio.h>
69 #define num 1005
70 double s[num];
71 int main()
72 {
73     int n, t, i;
74     while (scanf("%d", &n) == 1 && n)
75     {
76         double sum = 0, resultH = 0, resultL = 0;
77         for (i = 0; i < n; i++)
78         {
79             scanf("%lf", &s[i]);
80             sum += s[i];
81         }
82         sum /= n;
83
84         for (i = 0; i < n; i++)
85             if (s[i] < sum)
86             {
87                 resultL += (int)((sum - s[i])*100) / 100.0;
88             }
89             else
90                 resultH += (int)((s[i] - sum)*100) / 100.0;
91         printf("$%0.2lf\n", resultL < resultH ? resultH:resultL);
92     }
93     return 0;
94 }
时间: 2024-10-16 09:59:08

1301. The Trip的相关文章

POJ 1041 John&#39;s trip 无向图的【欧拉回路】路径输出

欧拉回路第一题TVT 本题的一个小技巧在于: [建立一个存放点与边关系的邻接矩阵] 1.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为偶数 有向图: 欧拉回路:连通 + 所有点的入度 == 出度 欧拉路径:连通 + 源点 出度-入度=1 && 终点 入度 - 出度 = 1 && 其余点 入度 == 出度: 2.求欧拉路径 : step 1:选取起点(如果是点的度数全为偶数任意点为S如果有两个点的度数位奇数取一

poj1734 Sightseeing trip

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6919   Accepted: 2646   Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attra

HDU 1038[Biker&#39;s Trip Odometer]简单计算

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1038 题目大意:给轮子直径,转数,时间.要求输出走过的距离和时速(mile为单位) 关键思想:纯算 代码如下: #include <iostream> using namespace std; #define pi 3.1415927 int main(){ double d,r,t,l; int cnt=1; while(cin>>d>>r>>t&&a

暑假练习赛 003 F Mishka and trip

F - Mishka and trip Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description Input Output Sample Input Sample Output Hint Description Peter Parker wants to play a g

1999 Central European Olympiad in Informatics - Sightseeing Trip

算法提示 最小环问题 题目大意 在一张带权无向图上,找出至少含 3 个点且权值和最小的环,并按环上的循序输出环上的点.存在重边,无自环. 做法分析 参考最小环问题,在更新 dist[i][j] 时,记录更新其的点 k,便于回溯路径. 参考代码 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <queue> 5 #include <algorithm> 6

三分 --- POJ 3301 Texas Trip

Texas Trip Problem's Link:   http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形的面积. analyse: 首先要确定的是旋转的角度在0到180度之间即可,超过180度是和前面的相同的. 坐标轴旋转后,坐标变换为: X’ = x * cosa - y * sina; y’ = y * cosa + x * sina; Time complexity: O(n) Source code

杭电 HDU 1038 Biker&#39;s Trip Odometer

Biker's Trip Odometer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4745    Accepted Submission(s): 3144 Problem Description Most bicycle speedometers work by using a Hall Effect sensor faste

POJ 1041 John&#39;s trip (无向图欧拉回路)

John's trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7433   Accepted: 2465   Special Judge Description Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his frien

Hdu 1301 Jungle Roads (最小生成树)

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1301 很明显,这是一道“赤裸裸”的最小生成树的问题: 我这里采用了Kruskal算法,当然用Prim算法也一样可以解题. #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; typedef struct node{ int f