#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. LeetCode Triangle 三角形(最短路)

    题意:给一个用序列堆成的三角形,第n层的元素个数为n,从顶往下,每个元素可以选择与自己最近的两个下层元素往下走,类似一棵二叉树,求最短路. [], [,4], [6,,7], [4,,8,3] 注意: ...

  2. SQL 2005 日志损坏的恢复方法

    SQL 在突然停电或者非正常关机下,可能会出现日期文件错误,导致数据库不正常.恢复数据库方法如下 1.数据库服务停掉 将数据库文件备份 例如数据库名为 DTMS 则将 DTMS.mdf 备份出来. 2 ...

  3. HDU 5407 CRB and Candies

    题意:给一个正整数k,求lcm((k, 0), (k, 1), ..., (k, k)) 解法:在oeis上查了这个序列,得知答案即为lcm(1, 2, ..., k + 1) / (k + 1),而 ...

  4. [Everyday Mathematics]20150128

    求极限 $$\bex \lim_{x\to 0}\sex{\frac{e^x+e^{2x}+\cdots+e^{nx}}{n}}^\frac{1}{x}. \eex$$

  5. 动态 SQL

    MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉 ...

  6. Storm的本地运行模式示例

    以word count为例,本地化运行模式(不需要安装zookeeper.storm集群),maven工程, pom.xml文件如下: <project xmlns="http://m ...

  7. 基于Fragment实现Tab的切换,滑出侧边栏

    最近在学习Fragment(碎片)这是android3.0以后提出的概念,很多pad上面的设置部分都是通过Fragment来实现的,先看看具体的效果吧(图一)  (图二) (图三)第一章图片是初始时的 ...

  8. JAVA和C/C++之间的相互调用。

    在一些Android应用的开发中,需要通过JNI和 Android NDK工具实现JAVA和C/C++之间的相互调用. Java Native Interface (JNI)标准是java平台的一部分 ...

  9. cxf 动态创建客户端,局域网能正常调用服务端,外网不能访问

  10. 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point

    // 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point // 思路:直接暴力绝对T // 先确定x范围,每个x范围内,离圆心最远的点一定是y轴两端的点.枚举x的范围,再 ...