UVa Problem Solution: 10137 - The Trip


I convert all the calculations from floating-point to integral to avoid all the messes in floating-point calculation. The method is straight forward: I simulate the exchanging progress and use a 'budget' to record the extra cents that should be exchanged to keep the costs in balance. 

Code:
  1. /*************************************************************************
  2.  * Copyright (C) 2008 by liukaipeng                                      *
  3.  * liukaipeng at gmail dot com                                           *
  4.  *************************************************************************/
  5. /* @JUDGE_ID 00000 10137 C "The Trip" */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <strings.h>
  10. #define MAX 1000
  11.         
  12. int calc_exchange(int costs[], int persons) 
  13. {
  14.   int total = 0, aver, exchange, budget;
  15.   int i;
  16.   for (i = 0; i < persons; ++i)
  17.     total += costs[i];
  18.   aver = total / persons;
  19.   exchange = 0;
  20.   budget = 0;
  21.   if (total % persons != 0)
  22.     aver += 1;
  23.   for (i = 0; i < persons; ++i) {
  24.     if (costs[i] >= aver) {
  25.       exchange += costs[i] - aver;
  26.       budget += costs[i] - aver;
  27.     } else if (costs[i] < aver - 1) {
  28.       budget -= aver - 1 - costs[i];
  29.     }
  30.   }
  31.   if (budget < 0)
  32.     exchange -= budget;
  33.   return exchange;
  34. }       
  35. int main(int argc, char *argv[])
  36. {
  37. #ifndef ONLINE_JUDGE
  38.   char in[256];
  39.   char out[256];
  40.   strcpy(in, argv[0]);
  41.   strcat(in, ".in");
  42.   freopen(in, "r", stdin);
  43.   strcpy(out, argv[0]);
  44.   strcat(out, ".out");
  45.   freopen(out, "w", stdout);
  46. #endif
  47.   
  48.   int costs[MAX];
  49.   char buf[20];
  50.   int len;
  51.   char c;
  52.   int persons, i, j;
  53.   int exchange;
  54.   for (scanf("%d/n", &persons); persons != 0; scanf("%d/n", &persons)) {
  55.     for (i = 0; i < persons; ++i) {
  56.       for (j = 0; (c = getchar()) != '/n'; ++j) {
  57.         if (c != '.')
  58.           buf[j] = c;
  59.         else
  60.           --j;
  61.       }
  62.       buf[j] = '/0';
  63.       costs[i] = atoi(buf);
  64.     }
  65.     exchange = calc_exchange(costs, persons);
  66.     sprintf(buf, "%d", exchange);
  67.     len = strlen(buf);
  68.     if (len == 1)
  69.       printf("$0.0%s/n", buf);
  70.     else if (len == 2)
  71.       printf("$0.%s/n", buf);
  72.     else {
  73.       putchar('$');
  74.       for (i = 0; i < len - 2; ++i)
  75.         putchar(buf[i]);
  76.       printf(".%s/n", buf+len-2);
  77.     }
  78.   }
  79.   return 0;
  80. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值