Pages

Wednesday 24 October 2012

UVA - 10137 - The Trip

#include <stdio.h>

int main() {
    int n,i;
    double sum=0,avg=0;
      while(1){
            scanf("%d",&n);
            double arr[n];
            if(n==0)
                break;
            sum=0;
            avg=0;
            for(i=0;i<n;i++){
                scanf("%lf",&arr[i]);
                sum+=arr[i];
            }
            avg=sum/n;

            double sum1=0,sum2=0,dif=0;
            for(i=0;i<n;i++){
                dif=(double)(long)((arr[i] - avg) * 100) / 100.0;
                if(dif<0)
                    sum2-=dif;
                else
                    sum1+=dif;
            }
            if(sum2>sum1){
                printf("$%.2lf\n", sum2);
            }else{
                printf("$%.2lf\n", sum1);
            }
          }
    return 0;
}


2 comments:

  1. dif=(double)(long)((arr[i] - avg) * 100) / 100.0;
    is this important to multiply by 100 then divide vy 100..?

    ReplyDelete
    Replies
    1. it just a way to make a precision don't care about it .... ;)
      the logic is the same ....

      Delete