关于UVa110103题目的分析

提交了几次,都是WA,查看程序觉得没有什么问题,上网查看别人家的程序,思想也都类似,瞬间感觉无厘头~接连提交了几次,发现还是错误。后来把float类型改成了double类型,就通过的~可能受到精度的影响,导致程序的测试用例结果输出错误了。 以此篇告诫自己。


题目如下: (转载自http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110103&format=html)


A group of students are members of a club that travels annually to differentlocations. Their destinations in the past have included Indianapolis, Phoenix, Nashville, Philadelphia, San Jose, and Atlanta. This spring they are planning a tripto Eindhoven.

The group agrees in advance to share expenses equally, but it is notpractical to share every expense as it occurs. Thus individualsin the group pay for particular things, such as meals, hotels, taxi rides,and plane tickets. After the trip, each student's expenses are talliedand money is exchanged so that the net cost to each is the same, to withinone cent. In the past, this money exchange has been tedious and timeconsuming. Your job is to compute, from a list of expenses, the minimumamount 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 thenumber of students on the trip.This is followed by n lines of input, each containingthe amount spent by a student in dollars and cents.There are no morethan 1000 students and no student spent more than $10,000.00. A singleline containing 0 follows the information for the last trip.

Output

For each trip, output a line stating the total amount of money, in dollarsand 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


1.最开始的时候我就注意到0.01的精度要求,前期也考虑了这个因素,但是在题目解决过程中,不知不觉又忽略的这个问题,不记笔记要点,不该啊。

2.关于题目的分析,因为允许误差0.01,而且精度也是0.01,所以分析上面,sum可以给人数n整除,又或者余下数值rest_num(rest_num<0.01*n):这里,我们记pnum=rest_num/0.01;这样子的话,sum价值的分布因该是pnum个aver+0.01,其他为aver。 

举例来说:15.0 15.01 3.0 3.01 其中sum=36.02 aver=9.00 rest_num=0.02 得到pnum=2;这样子,4个人交换之后,费用应该是 2个9.01,4-2个9.00;符合要求,也符合


出于上面的分析。源码如下:

#include <iostream>
#include<cstdio>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

int main(int argc, char** argv) {
    int n;
    double payment[1010];
    char temp[1010];
    while(cin>>n&&n){
        double sum=0.0;
        for(int i=0;i<n;i++){
            cin>>payment[i];
            sum+=payment[i];
        }
        double aver=((int)(sum*100))/n/100.0;
        
    //    cout<<aver<<endl;
        sprintf(temp,"%.2lf",aver);
        sscanf(temp,"%lf",&aver);
    //    cout<<aver<<endl;
        
        double ans=0.0;
        int count=0;
        for(int i=0;i<n;i++){
            if(payment[i]>aver+0.01){
                ans+=(payment[i]-aver-0.01);
                count++;
            }
        }
        if(count*0.01>sum-aver*n)
            ans+=(count*0.01-sum+aver*n);
            
        printf("$%.2lf\n",ans);
        
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值