關於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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章