The Trip

来源:互联网 发布:mysql 32位下载安装包 编辑:程序博客网 时间:2024/04/28 13:27
110103 The Trip
A group of students are members of a club that travels annually to di erent 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 share every expense
as it occurs. Thus individuals in the group pay for particular things, such as meals, hotels, taxi rides,
and plane tickets. 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 one cent) all the students'costs.
Input
Standard input will contain the information for several trips. Each trip consists of a line containing
a positive integer n denoting the number of students on the trip. This is followed by n lines of input,
each containing the amount spent by a student in dollars and cents. 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


思路:首先想到的是要想知道交易总和最少,我必须解出平均每个人出多少,平均数解出来,根据题意精确到1美分,即0.01,然后根据多退少补的方式,求得结果,一个细节是,我们要考虑四种情况:情况一:当多退少补都为零;情况二:当多退为零,少补不为零时;情况三:当少补为零,多退不为零时;四:当多退少补都为零。


个人代码:

#include <stdio.h>        #define SIZE 1000       double findChange(double *money, int total)       {        double average = 0.0, changeDown = 0.0, changeUp = 0.0, change = 0.0;               int i;            for (i = 0; i < total; i++)               average += money[i];                   average /= total;          average = (long) (average * 100 + 0.5) / 100.00;          for (i = 0; i < total; i++)      {          if (money[i] > average)              changeUp += (money[i] - average);               else              changeDown += (average - money[i]);          }          if (changeDown > 0 && changeUp > 0)      {          if (changeDown > changeUp)                 change = changeUp;              else              change = changeDown;      }      else  //如果一边有零     {          if (changeUp == 0 && changeDown == 0)//两边都为零                    change = 0.0;          else          {              if (changeDown == 0)//少补为零时              {                  for ( i = 0; i < total; i++)                      if (money[i] > average)                          change +=                              (money[i] - average -                              0.01);              }              else  //多退为零时            {                  for ( i = 0; i < total; i++)                      if (money[i] < average)                          change +=                              (average - money[i] -                             0.01);            }           }      }        return change;  }    int main()  {           double money[SIZE], result = 0.0;          int total,i;        while (scanf("%d",&total) != 0)      {          for ( i = 0; i < total; i++)              scanf("%f",&(money[i]));            result = findChange(money, total);                printf("$%0.2f\n",result);    }        return 0;  }  

正确代码:

#include <stdio.h>float get_aveg(float a);int main(){     int count;     int i;     float sum,aveg,changeup,changedown,result;         while(scanf("%d",&count) != 0 && 0 < count && count < 1000){                  sum = 0.00;                  result = 0.00;            float fate[count];               for (i =0;i < count;i++){                  scanf("%f",&fate[i]);                  while(fate[i] > 100000)                     scanf("%f",&fate[i]);               }              for (i =0;i < count;i++){                 sum += fate[i];                   }                               aveg = sum/count;                 aveg = get_aveg(aveg);              for (i =0;i < count;i++){                  if(fate[i] > aveg)                     changeup += (fate[i]- aveg);                  if(fate[i] < aveg)                     changedown +=(aveg - fate[i]);                      }            if (changedown > 0 && changeup > 0)           {              if (changedown > changeup)                     result = changeup;                  else                result = changedown;          }      else  //如果一边有零     {          if (changeup == 0 && changedown == 0)//两边都为零                    result = 0.0;          else          {              if (changedown == 0)//少补为零时              {                  result = 0.00;                    /*for ( i = 0; i < count; i++)                      if (fate[i] > aveg)                          result +=                              (fate[i] - aveg -                              0.01);                    */                      }              else  //多退为零时            {                  result = 0.00;                    /*for ( i = 0; i < count; i++)                      if (fate[i] < aveg)                          result +=                              (aveg - fate[i] -                             0.01);                    */                                       }           }      }                                           printf("$%0.2f",result);             }                   }float get_aveg(float a){         int b ;         float c;            b = a*1000;            if(b % 10 >= 5){                 b= b/10 + 1;                 c = (float)b /100;                 }            else {                 b = b/10;                 c = (float)b /100;                 }           return c;            }    


目前根据测试并没有错误,但是提交出现WA。暂未解决。


原创粉丝点击