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
下面是我对他的代码的理解,说实话,看别人代码果然能开拓视野,提高自己的水平。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; #define maxn 1005 int n;
long long sum, f[maxn], f1[maxn]; int main()
{
//freopen("t.txt", "r", stdin);
while (scanf("%d", &n), n)
//第一次看到这种写法,括号里面是一个逗号表达式,逗号表达式的值和类型是最后一个表达式的值和类型。另注意和while(scanf("%d",&n) && n)的区别。
{
sum = ;
for (int i = ; i < n; i++)
{
double a;
scanf("%lf", &a);
f[i] = (long long)(a * + 0.5);
sum += f[i];
}
sort(f, f + n);
reverse(f, f + n);
long long a, ave;
ave = sum / n;
a = sum % n;
for (int i = ; i < n; i++)
f1[i] = ave;
for (int i = ; i < a; i++)
f1[i] += ;
long long ans = ;
for (int i = ; i < n; i++)
if (f[i] > f1[i])
ans += f[i] - f1[i];
printf("$%.2f\n", double(ans / 100.0));
}
return ;
}

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

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

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

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

My code:

 #include <iostream>
#include <stdio.h>
using namespace std; int main()
{
int n;
double stu[];
while(cin>>n){
if(n==)
break;
double ave=;
for(int i=;i<=n;i++){
cin>>stu[i];
ave+=stu[i];
}
ave=ave/n;
ave=int (ave*+0.5)/100.0; double stu1=,stu2=;
for(int i=;i<=n;i++){
if(stu[i]>=ave)
stu1+=stu[i]-ave;
else
stu2+=ave-stu[i];
}
if(stu1<stu2)
printf("$%.2lf\n",stu1);
else
printf("$%.2lf\n",stu2);
}
return ;
}

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. iOS Xcode 调试技巧 全局断点这样加才有意思

    http://blog.sina.com.cn/s/blog_876a2c9901016ezh.html

  2. 锋利的jQuery-7--$.extend()

    $.extend()主要有两个功能: 1.扩展jQuery对象 jQuery.extend({ min: function(a, b) { return a < b ? a : b; }, }) ...

  3. Linux之入侵痕迹清理总结

    rm -f -r /var/log/*rm .bash_historyrm recently_used

  4. Linux 下的另一个密码破解工具medusa

    首先,本人在此声明 此工具许合理利用非法破解很可能会被发现的因为这是一个暴力破解方式需要不断的尝试登陆服务器 ,服务器上的检测软件很快可以跟踪到并锁定你的IP地址 请大家切勿用于非法手段- -! me ...

  5. windows下使用批处理文件调用python程序

    这个随笔涉及到几个批处理脚本得知识点. windows的start命令, 启动另一个窗口运行指定的程序或命令. windows的call命令, 从批处理程序调用另一个程序, 直到被调用程序退出, 再继 ...

  6. Zhulina 的高分子刷理论

    高分子刷的解析平均场理论有两种表述方式.一个是MWC理论(Macromolecules 1988, 21, 2610-2619),另外一个就是Zhulina和Birshtein这两位俄罗斯老太太的理论 ...

  7. Linux下使用fdisk扩展分区容量

    导读 我们管理的服务器可能会随着业务量的不断增长造成磁盘空间不足的情况,比如:共享文件服务器硬盘空间不足,在这个时候我们就需要增加磁盘空间,来满足线上的业务:又或者我们在使用linux的过程中, 有时 ...

  8. ssh和mvc理论基础

    ssh中mvc到底指的什么 mvcsshhibernatespringstrutsioc在SSH整合的架构中,Spring充当了一个容器的作用,Spring使用IOC和AOP技术接管了Hibernat ...

  9. UITapGestureRecognizer

    UITapGestureRecognizer IOS的手势非常多, 但是特别容易其他视图起冲突的手势,要数UITapGestureRecognizer 于是有了gestureRecognizerSho ...

  10. 对target="framename"的理解(实现分页的demo)

    先上图,说明一下我主要想实现什么功能. 一.演示图 演示首页: 演示内容页(包括按钮切换页+模板内容页): 演示首页到演示内容页的一个演变过程: