#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<iterator>

using namespace std;
/*
* A solution for "The Trip" problem.
* UVa ID: 10137
*/
#include <stdio.h>

int main (int argc, const char * argv[]) {
    /* number of students in the trip */
    long numOfStudents;
   
    /* the total sum of money spent */
    double total;
   
    /* the total amount of money to exchange in order to equalize */
    double exchange;
   
    /* the equalized trip amount to be payed by each student */
    double equalizedAmount;
   
    /* difference between the equalized amount and the amount spent */
    double diff;
   
    /* sum of all negative differences */
    double negativeSum;
   
    /* sum of all positive differences */
    double positiveSum;
   
    /* iterator */
    int i;
   
    while(scanf("%ld", &numOfStudents) != EOF) {
               
        /* 0, ends the program */
        if (!numOfStudents) {
            return 0;
        }
       
        /* keeps the amount of money spent by each student */
        double amountSpent[numOfStudents];     
       
        /* clean */
        total = 0;
        negativeSum = 0;
        positiveSum = 0;
               
        for (i = 0; i < numOfStudents; i++) {
            scanf("%lf\n", &amountSpent[i]);
            total += amountSpent[i];
        }
               
        equalizedAmount = total / numOfStudents;
       
        for (i = 0; i < numOfStudents; i++) {
            /* to ensure 0.01 precision */
            diff = (double) (long) ((amountSpent[i] - equalizedAmount) * 100.0) / 100.0;
           
            if (diff < 0) {
                negativeSum += diff;
            } else {
                positiveSum += diff;
            }
        }

        /* when the total amount is even, these sums do not differ. otherwise, they differ in one cent */
        exchange = (-negativeSum > positiveSum) ? -negativeSum : positiveSum;
                       
        /* output result */
        printf("$%.2lf\n", exchange);
    }

    return 0;
}

The Trip PC/UVa IDs: 110103/10137, Popularity: B, Success rate: average Level: 1的更多相关文章

  1. Minesweeper PC/UVa IDs: 110102/10189, Popularity: A,Success rate: high Level: 1

    #include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...

  2. PC/UVa 题号: 110106/10033 Interpreter (解释器)题解 c语言版

    , '\n'); #include<cstdio> #include<iostream> #include<string> #include<algorith ...

  3. 挑战编程PC/UVa Stern-Brocot代数系统

    /* Stern-Brocot代数系统 Stern-Brocot树是一种生成所有非负的最简分数m/n的美妙方式. 其基本方式是从(0/1, 1/0)这两个分数开始, 根据需要反复执行如下操作: 在相邻 ...

  4. PC/UVa 题号: 110105/10267 Graphical Editor (图形化编辑器)题解

    #include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...

  5. PC/UVa 题号: 110104/706 LC-Display (液晶显示屏)题解

    #include <string> #include <iostream> #include <cstring> #include <algorithm> ...

  6. PC/UVa 题号: 110101/100 The 3n+1 problem (3n+1 问题)

     The 3n + 1 problem  Background Problems in Computer Science are often classified as belonging to a ...

  7. UVa 122 (二叉树的层次遍历) Trees on the level

    题意: 输入一颗二叉树,按照(左右左右, 节点的值)的格式.然后从上到下从左到右依次输出各个节点的值,如果一个节点没有赋值或者多次赋值,则输出“not complete” 一.指针方式实现二叉树 首先 ...

  8. Uva 122 树的层次遍历 Trees on the level lrj白书 p149

    是否可以把树上结点的编号,然后把二叉树存储在数组中呢?很遗憾如果结点在一条链上,那将是2^256个结点 所以需要采用动态结构 首先要读取结点,建立二叉树addnode()+read_input()承担 ...

  9. uva 704

    自己之前的不见了.. 这题是双向广搜即可过.. // Colour Hash (色彩缤纷游戏) // PC/UVa IDs: 110807/704, Popularity: B, Success ra ...

随机推荐

  1. 【C#学习笔记】读SQL Server2008

    using System; using System.Data.SqlClient; namespace ConsoleApplication { class Program { static voi ...

  2. UVa 11300 Spreading the Wealth 分金币

    圆桌旁坐着 n 个人,每个人都有一定数量的金币,金币总数能够被 n 整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值,比如 n = 4, ...

  3. android中ViewHolder通用简洁写法

    public class ViewHolder {     // I added a generic return type to reduce the casting noise in client ...

  4. 【应聘】阿里巴巴Java面试题目

    原文地址:http://blog.csdn.net/free0sky/article/details/7927275   一.String,StringBuffer, StringBuilder 的区 ...

  5. (原创)LAMP教程3-下载centos6.4

    (原创)LAMP教程3-下载centos6.4 今天我要给大家讲的是安装CentOS-6.4-x86_64,是的没有错,就是64位的,因为我的机子是4G的内存,安装64位的centos是绰绰有余啊,但 ...

  6. Cutting Sticks

    题意: l长的木棒,给出n个切割点,每切一次的费用为切得木棒的长度,完成切割的最小费用. 分析: 区间dp入门,区间dp的特点,一个大区间的解可以转换成小区间的解组合起来,每个切割点的标号代表边界. ...

  7. 浅谈javascript的变量作用域

    1.变量遵循先声明再使用. console.log(b); b=123; 代码运行结果: Uncaught ReferenceError: b is not defined 2.方法内定义的局部变量外 ...

  8. aspx与ascx,ashx的用法详细的总结介绍

    这篇文章主要是对aspx与ascx,ashx的用法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助 做asp.net开发的对.aspx,.ascx和.ashx都不会陌生.关于它们,网 ...

  9. XSLT 调用java

    XSLT调用JS  http://www.ibm.com/developerworks/cn/xml/tips/x-tipxsltjs/index.htmlXSLT调用JAVA  http://unm ...

  10. 在Ubuntu6.06 在搭建SVN服务器及在windows建立svn+ssh客户端 (续)

    接上篇.本篇主要介绍windows下建立svn+ssh客户端. 9.在windows下安装svn客户端,则需安装“TortoiseSVN”.“Puttygen”和“Pageant”    9.1.下载 ...