zoukankan      html  css  js  c++  java
  • The Trip PC/UVa IDs: 110103/10137, Popularity: B, Success rate: average Level: 1

    #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 ", &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 ", exchange);
        }

        return 0;
    }

  • 相关阅读:
    数据仓库- 建模理念
    SpringBoot- springboot集成Redis出现报错:No qualifying bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory'
    CDH- cdh kafka已经卸载了,但是服务器还有kafka-topics这些命令可用,导致重新安装kafka出现问题
    【JZOJ6217】【20190614】最大面积
    【JZOJ6216】【20190614】序列计数
    【JZOJ6228】【20190621】ni
    【JZOJ6227】【20190621】ichi
    【JOISC2019|2019】【20190622】cake3
    【JOISC2018|2019】【20190622】mergers
    【JOISC2018|2019】【20190622】minerals
  • 原文地址:https://www.cnblogs.com/cute/p/3384100.html
Copyright © 2011-2022 走看看