博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
烟大 Contest1024 - 《挑战编程》第一章:入门 Problem C: The Trip(水题)
阅读量:6836 次
发布时间:2019-06-26

本文共 3608 字,大约阅读时间需要 12 分钟。

Problem C: The Trip

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

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

310.0020.0030.00415.0015.013.003.010

Sample Output

$10.00$11.99

HINT


 

卡在这道题上好久,总有几组测试数据不通过。

遂参考了一份网上的结题报告。
下面是Rainy Days的博客上这道题的代码,这是位牛人,粗算已经做过700+的题,佩服,向前辈学习!
下面是我对他的代码的理解,说实话,看别人代码果然能开拓视野,提高自己的水平。

 

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 9 #define maxn 100510 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\n", 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 
2 #include
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 else25 stu2+=ave-stu[i];26 }27 if(stu1

 

 Freecode :

转载地址:http://tqqkl.baihongyu.com/

你可能感兴趣的文章
替换libc中的内存分配函数
查看>>
myeclipse8.6安装svn
查看>>
序列化和反序列化二叉搜索树 Serialize and Deserialize BST
查看>>
批量去除歌曲tag标签
查看>>
驰骋工作流引擎设计系列05 启动流程设计
查看>>
Java 启动线程并保持
查看>>
CentOS7使用firewalld打开关闭防火墙与端口
查看>>
开启mysql远程访问的权限
查看>>
st2045 漏洞反弹root shell
查看>>
Debian 系统初体验
查看>>
将Unreal4打包后的工程嵌入到Qt或者桌面中
查看>>
TP 框架没有考虑完善的功能点:1、表达式查询不支持INSTR形式的查询
查看>>
你不可不知的家庭装修禁忌
查看>>
关于i++和++i
查看>>
如何处理win10系统内置Linux系统闪退问题
查看>>
在Ubuntu上通过命令行安装Elisa KDE音乐播放器
查看>>
用grep命令查找文件中带特定扩展名的字符串
查看>>
显示串中只出现一次的字符.
查看>>
为创世纪图书馆(Library Genesis)作镜像
查看>>
不错的学习网站
查看>>