zoukankan      html  css  js  c++  java
  • 烟大 Contest1024

    Problem C: The Trip

    Time Limit: 1 Sec  Memory Limit: 64 MB
    Submit: 19  Solved: 3
    [Submit][Status][Web Board]

    Description

    The Trip A group of students are members of a club that travels annually to different 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

    HINT

    卡在这道题上好久,总有几组测试数据不通过。
    遂参考了一份网上的结题报告。
    下面是Rainy Days的博客上这道题的代码,这是位牛人,粗算已经做过700+的题,佩服,向前辈学习!
    http://www.cnblogs.com/rainydays/archive/2011/07/07/2100471.html
    下面是我对他的代码的理解,说实话,看别人代码果然能开拓视野,提高自己的水平。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cmath>
     6 #include <algorithm>
     7 using namespace std;
     8 
     9 #define maxn 1005
    10 
    11 int n;
    12 long long sum, f[maxn], f1[maxn];
    13 
    14 int main()
    15 {
    16     //freopen("t.txt", "r", stdin);
    17     while (scanf("%d", &n), n)    
    18     //第一次看到这种写法,括号里面是一个逗号表达式,逗号表达式的值和类型是最后一个表达式的值和类型。另注意和while(scanf("%d",&n) && n)的区别。
    19     {
    20         sum = 0;
    21         for (int i = 0; i < n; i++)
    22         {
    23             double a;
    24             scanf("%lf", &a);
    25             f[i] = (long long)(a * 100 + 0.5);
    26             sum += f[i];
    27         }
    28         sort(f, f + n);
    29         reverse(f, f + n);
    30         long long a, ave;
    31         ave = sum / n;
    32         a = sum % n;
    33         for (int i = 0; i < n; i++)
    34             f1[i] = ave;
    35         for (int i = 0; i < a; i++)
    36             f1[i] += 1;
    37         long long ans = 0;
    38         for (int i = 0; i < n; i++)
    39             if (f[i] > f1[i])
    40                 ans += f[i] - f1[i];
    41         printf("$%.2f
    ", double(ans / 100.0));
    42     }
    43     return 0;
    44 }
    View Code

    下面是我自己的代码,在这道题上卡了有一段时间,原因是题意没搞清楚。

    重点就是四舍五入(*100+0.5,取整),之后赊的钱和多交的钱的总数取较小的那个输出。

    这道题一不小心,很容易产生误差而WA。所以做的时候要谨慎啊!

    PS:发现一个细节上的问题,float型以及double型数据存储像900.5这样的数的时候,实际存储的时候是900.499999……因此只要>0.5四舍五入就会成功,而恰好等于0.5这样的数会失败,我不知道这道题的测试数据是怎样,可能等于0.5的时候恰好对了,而大部分测试数据都是>0.5的情况,没有给予考虑,我不知道这算不算个bug。

    My code:

     1 #include <iostream>
     2 #include <stdio.h>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     int n;
     8     double stu[1001];
     9     while(cin>>n){
    10         if(n==0)
    11             break;
    12         double ave=0;
    13         for(int i=1;i<=n;i++){
    14             cin>>stu[i];
    15             ave+=stu[i];
    16         }
    17         ave=ave/n;
    18         ave=int (ave*100+0.5)/100.0;
    19 
    20         double stu1=0,stu2=0;
    21         for(int i=1;i<=n;i++){
    22             if(stu[i]>=ave)
    23                 stu1+=stu[i]-ave;
    24             else
    25                 stu2+=ave-stu[i];
    26         }
    27         if(stu1<stu2)
    28             printf("$%.2lf
    ",stu1);
    29         else
    30             printf("$%.2lf
    ",stu2);
    31     }
    32     return 0;
    33 }

     Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    CSS画出三角形(利用Border)
    Javascript中style,currentStyle和getComputedStyle的区别以及获取css操作方法
    用canvas实现验证码的绘制
    tinymce富文本编辑器升级问题
    同步和异步
    this
    发送短信——案例
    SpringMvc框架【多测师】
    通过百度文字识别的API来实现把图片内容写入到txt文件当中【多测师】
    史上最全软件测试工程师常见的面试题总结(七)【多测师】
  • 原文地址:https://www.cnblogs.com/yym2013/p/3227003.html
Copyright © 2011-2022 走看看