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

  1. 3
  2. 10.00
  3. 20.00
  4. 30.00
  5. 4
  6. 15.00
  7. 15.01
  8. 3.00
  9. 3.01
  10. 0

Sample Output

  1. $10.00
  2. $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 = ;
  21. for (int i = ; i < n; i++)
  22. {
  23. double a;
  24. scanf("%lf", &a);
  25. f[i] = (long long)(a * + 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 = ; i < n; i++)
  34. f1[i] = ave;
  35. for (int i = ; i < a; i++)
  36. f1[i] += ;
  37. long long ans = ;
  38. for (int i = ; i < n; i++)
  39. if (f[i] > f1[i])
  40. ans += f[i] - f1[i];
  41. printf("$%.2f\n", double(ans / 100.0));
  42. }
  43. return ;
  44. }

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

重点就是四舍五入(*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[];
  9. while(cin>>n){
  10. if(n==)
  11. break;
  12. double ave=;
  13. for(int i=;i<=n;i++){
  14. cin>>stu[i];
  15. ave+=stu[i];
  16. }
  17. ave=ave/n;
  18. ave=int (ave*+0.5)/100.0;
  19.  
  20. double stu1=,stu2=;
  21. for(int i=;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\n",stu1);
  29. else
  30. printf("$%.2lf\n",stu2);
  31. }
  32. return ;
  33. }

Freecode : www.cnblogs.com/yym2013

烟大 Contest1024 - 《挑战编程》第一章:入门 Problem C: The Trip(水题)的更多相关文章

  1. ACM YTU 《挑战编程》第一章 入门 Problem E: Graphical Editor

    Description Graphical editors such as Photoshop allow us to alter bit-mapped images in the same way ...

  2. Windows核心编程第一章.错误处理

    Windows核心编程第一章,错误处理. 一丶错误处理 1.核心编程学习总结 不管是做逆向,开始做开发.在Windows下.你都需要看一下核心编程这本书.这本书确实写得很好.所以自己在学习这本书的同时 ...

  3. .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划

    作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9977862.html 写在前面 千呼万唤始出来,首先,请允许我长吸一口气!真没想到一份来自28岁老程序员 ...

  4. net core体系-web应用程序-4asp.net core2.0 项目实战(CMS)-第一章 入门篇-开篇及总体规划

    .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划   原文地址:https://www.cnblogs.com/yilezhu/p/9977862.html 写在前面 千呼万唤始出来 ...

  5. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem A: The 3n + 1 problem(水题)

    Problem A: The 3n + 1 problem Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 14  Solved: 6[Submit][St ...

  6. Java 面向对象编程——第一章 初识Java

      第一章    初识Java 1.  什么是Java? Java是一种简单的.面向对象的.分布式的.解释的.安全的.可移植的.性能优异的多线程语言.它以其强安全性.平台无关性.硬件结构无关性.语言简 ...

  7. Java学习笔记 第一章 入门<转>

    第一章 JAVA入门 一.基础常识 1.软件开发 什么是软件? 软件:一系列按照特定顺序组织的计算机数据和指令的集合 系统软件:DOS,Windows,Linux 应用软件:扫雷.QQ.迅雷 什么是开 ...

  8. windows核心编程-第一章 对程序错误的处理

    第一章-对程序错误的处理 在开始介绍Microsoft Windows 的特性之前,必须首先了解 Wi n d o w s的各个函数是如何进行错误处理的. 当调用一个Wi n d o w s函数时,它 ...

  9. UNIX环境高级编程--第一章 UNIX基础知识

    第一章 UNIX基础知识 1.2 UNIX体系结构   从严格意义上说,可将操作系统定义为一种软件,它控制计算机硬件资源,提供程序运行环境.我们将这种软件称为内核(kernel),因为 它相对较小,且 ...

  10. 读高性能JavaScript编程 第一章

    草草的看完第一章,虽然看的是译文也是感觉涨姿势了, 我来总结一下: 由于 大多数浏览器都是 single process 处理 ui updatas and js execute 于是产生问题: js ...

随机推荐

  1. spring bean实例化方式

    注意:xml配置中bean节点下scope属性默认值为singleton(单例),在需要多例的情况下需要配置成prototype spring提供三种实例化方式:默认构造.静态工厂.实例工厂 一.默认 ...

  2. [LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal

    前中后遍历 递归版 /* Recursive solution */ class Solution { public: vector<int> preorderTraversal(Tree ...

  3. Log4j使用教程 log4:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).

    1.Logger类 通过Logger类的静态方法Logger.getRootLogger得到RootLogger.所有其他的loggers是通过静态方法Logger.getLogger来实例化并获取的 ...

  4. [整理]Code::Blocks使用遇到的问题

    在使用其编写C小程序的过程总会遇到些问题,特整理如下: 1.无法调试 注意的是项目所在的文件路径不能包含中文. 2.头文件接口函数申明引用无效 查看头文件是否处于可编译状态,左侧项目文件列表里是文件名 ...

  5. 简单聊下IO复用

    没图,不分析API Java中IO API的发展:Socket -> SocketChannel -> AsynchronousSocketChannelServerSocket -> ...

  6. poj1013.Counterfeit Dollar(枚举)

    Counterfeit Dollar Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 415  Solved: 237 Description Sally ...

  7. springmvc 项目完整示例06 日志–log4j 参数详细解析 log4j如何配置

    Log4j由三个重要的组件构成: 日志信息的优先级 日志信息的输出目的地 日志信息的输出格式 日志信息的优先级从高到低有ERROR.WARN. INFO.DEBUG,分别用来指定这条日志信息的重要程度 ...

  8. 简单实现Tab切换(带框架)

    <script type="text/javascript"> $(function () { //加载时添加的标签卡 if ('<%=Request[" ...

  9. VS2008+Qt+助手 智能提示不显示、Qt关键字不高亮的解决办法【已解决】

    笔者使用的开发环境是VS2008+Qt4.8.5+VAssistX,有时候会出现代码关键字不能高亮显示,并且助手的智能提示不显示.问题如下 解决的办法是在助手的选项中设置其搜索路径,助手的设置通过VS ...

  10. Android 下载文件及写入SD卡

    Android 下载文件及写入SD卡,实例代码 <?xml version="1.0" encoding="utf-8"?> <LinearL ...