UVA10137旅行问题(费用平摊,求最小交易总额,精确到分,误差不超过一分)已提交UVA online 通过

//

//  main.cpp

//  uva10137

//

//  Created by mac on 12-10-28.

//  Copyright (c) 2012 Roc. All rights reserved.

//  程序已经在http://uva.onlinejudge.org/ 提交通过

//  10807450 10137The TripAcceptedC++0.0162012-10-28 10:16:21

#include <iostream>
using namespace std;
#define SIZE 1000
double uva110103(double array[],int len){
   double sum=0;
   for (int i=0; i<len;i++) {
        sum+=array[i];
    };
    //<<setiosflags(ios::fixed)<<setprecision(2)<<sum/len<<endl;
   double average = sum/len,theMinTotalTrade1=0.0f,theMinTotalTrade2=0.0f,theMinTotalTrade;
    // cout<<setiosflags(ios::fixed)<<setprecision(2)<<average<<endl;
    average = (long) (average *100 +0.5) /100.00; //这边的四舍五入是公平的
    
    //cout<<average<<endl;
    
   for (int i=0; i<len; i++) {
       if(average - array[i]>0)  theMinTotalTrade1+=average-array[i];
    }
   for (int i=0; i<len; i++) {
       if(average - array[i]<0)  theMinTotalTrade2+=array[i]-average;//负的总交易额
    }
    //cout<<setiosflags(ios::fixed)<<setprecision(2)<<theMinTotalTrade<<endl;
   if(theMinTotalTrade1<(theMinTotalTrade2))
    { 
        // cout<<setiosflags(ios::fixed)<<setprecision(8)<<theMinTotalTrade1<<endl;
        theMinTotalTrade=theMinTotalTrade1;
    }else {
        
        //cout<<setiosflags(ios::fixed)<<setprecision(8)<<theMinTotalTrade2<<endl;
        theMinTotalTrade=theMinTotalTrade2;     
    }
    //下面这步可能很诡异,但是为了少数查出所有和 "真正平均费用"超过一分钱的出入(不管多给还是少给)人作出的额外平衡操作.
    
   for (int i=0, average=sum/(double)len; i<len; i++) {
        
       if(average-array[i]>0 && average-array[i]>0.01&& average-array[i]<=0.03) theMinTotalTrade+=0.01;
       if(array[i]-average>0 && array[i]-average>0.01&& array[i]-average<=0.03) theMinTotalTrade+=0.01;
        
    };
    //cout<<setiosflags(ios::fixed)<<setprecision(2)<<theMinTotalTrade<<endl;    
   return theMinTotalTrade;   
    
}   

int main(int argc,constchar * argv[])
{
   double money[SIZE], result =0.0;  
   int total;  
    
   while (cin >> total, total)  
    {  
       for (int i =0; i < total; i++)  
           cin >> money[i];  
        
        result =uva110103(money, total);  
        
       cout.precision(2);  
        cout.setf(ios::fixed |ios::showpoint);  
       cout <<"$" << result <<endl;  
    }  
    
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值