UVA10137旅行有关问题(费用平摊,求最小交易总额,精确到分,误差不超过一分)已提交UVA online 通过
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; }