The Trip

From Progteam

Revision as of 15:40, 17 June 2008 by Kerry (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Checkmark.jpg This problem has been solved by kerry.



The Trip
Problem Number 2646
Sorter: kerry
Source: Unknown
Link: http://acm.pku.edu.cn/JudgeOnline/problem?id=2646



The Trip is problem number 2646 on the Peking University ACM site.

C answer. Float for the values leads to arithmetic errors, so you need a double.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>


int main() {
long double current, total, avg, upper_total, answer, input[1001];
int i, j, num, upper_num;

        scanf("%d", &num);
        while (num !=0) {
                current=0.0;
                total=0.0;
                avg=0.0;
                answer=0.0;
                upper_total=0.0;
                upper_num=0;
                for(i=0;i<num;i++) {
                        scanf("%Lf", &current);
                        input[i]=current;
                        total += current;
                }
                avg = (total/num)*100;
                avg = floor(avg);
                avg = (avg/100);
                for(i=0;i<num;i++) {
                        if (input[i]<avg)
                                answer+=(avg-input[i]);
                        else if (input[i]>avg) {
                                upper_total+=(input[i]-avg);
                                upper_num++;
                        }
                }
                if ((total - avg*num) > (upper_num*0.01)) {
                        answer += (total-avg*num-upper_num*0.01);
                }
                printf("$%.2Lf\n", answer);
                scanf("%d", &num);
        }
}

Personal tools