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

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

上一篇:ios用户控件


下一篇:ubuntu segmentation fault 段错误